Page 54 - DCAP404 _Object Oriented Programming
P. 54

Unit 2: Beginning of OOP Language




                 structone.c  =  30;                                                            Notes
                 classone.p  =  56;
                 classone.q  =  16;
                 classone.r  =  51;
                 classone.doit();
          Evidently, each member can be referenced by using (.) dot operator. However, (.) operator does
          not work on pointer or reference type variables, as is shown in the following code snippet.
                 abc  *ptrAbc;
                 pqr  *ptrPqr;
                 abc  abcone;
                 pqr  pqrone;

                 ptrAbc  =  abcone;    //pointer variable ptrAbc points to object abcone
                 ptrAbc->a;
                 ptrPqr->doit();





             Notes  It is possible to access the value of variables pointed by the pointer variables using
             pointer. This is performed by using the Dereference operator in C++ which has the notation
             *.

          2.6.1  Memory Management Operators


          Along with malloc(), calloc() and frec() functions, C++ also defines two unary operators new and
          delete  that  perform  the task  of allocating and  freeing  the  memory.  Since these  operators
          manipulate memory on the free store, they are also known as free store operators. A data object
          created inside a block with new will remain in existence until it is explicitly destroyed by using
          delete.
          The general form of the new operator
          pointer_variable  =  new  data_type;
          The pointer_variable holds the address of the memory space allocated.

          p  =  new  int;
          q  =  new  float;
          We can also initialize the memory using the new operator. This is done as follows:
          pointer_variable=  new  data_type(value);
          Value  specifies  the  initial  value  as  shown  below:
          int*p  =  new  int(25);
          When an object is no longer needed, it is destroyed to release the memory space for reuse. The
          general form is:
          delete  pointer_variable;






                                           LOVELY PROFESSIONAL UNIVERSITY                                   47
   49   50   51   52   53   54   55   56   57   58   59