Page 127 - DCAP404 _Object Oriented Programming
P. 127

Object-oriented Programming




                    Notes                          *num1 = ident1;
                                                   *num2 = ident2;
                                               }


                                           ~Calculator()
                                               {
                                                   delete num1;
                                                   delete num2;
                                               }


                                           int add ()
                                               {
                                                   int sum = *num1 + *num2;
                                                   return sum;
                                               }
                                       };
                                   int  main()
                                       {
                                                   Calculator  myObject(2,3);

                                                   int  result  =  myObject.add();
                                           cout << result;

                                           return 0;
                                       }
                                   The destructor function is automatically called, without you knowing, when the program no
                                   longer needs the object. If  you defined a destructor  function as in the above code, it will be
                                   executed. If you did not define a destructor function, C++ supplies you one, which the program
                                   uses unknown to you. However, this default destructor will not destroy dynamic objects.





                                     Notes  An object is destroyed as it goes out of scope.

                                   Self Assessment

                                   Fill in the blanks:
                                   1.  A constructor is a special method that is created when the …………………… is created or
                                       defined.
                                   2.  A constructor is declared without a return value, that also excludes………………..
                                   3.  You cannot declare a constructor as virtual or static, nor can you declare a constructor as
                                       …………………, volatile, or const volatile.




          120                               LOVELY PROFESSIONAL UNIVERSITY
   122   123   124   125   126   127   128   129   130   131   132