* New post Constructor Destructor and Inheritance& Array , Virtual base class in c++ language

Thursday, 10 April 2014

0 Constructor , Destructor and Inheritance in c++ language

Constructor and Inheritance
Constructor and inheritance : A constructor is a member function , It is used to initializes the object of the class . In inheritance both the base class and the derived class can contain a constructor function . When both the base class and derived class contain constructor function .Then after creating the object of derived class the base class constructor is executed and then the derived class constructor function is executed .
Program :
Class A 
         {                                             // www.programmingking.in
            Public : 
                      Int x , y , z ;
                      A ( )
                      {
                        x = 10 ;  y =20 ; z =30 ;
                        cout <<"x ="<<x<<"\t y ="<<y<<"\t z ="<<z<<end l ;
                      }
           };
           Class B : Public A
           {
              Public :
                      Int a ,b , c ;
                      B ( )
                      {
                           a = 40 ;  b =50 ; c =60 ;
                        cout <<"a ="<<a<<"\t b ="<<b<<"\t c ="<<c<<end l ;
                      }
       };
        Void main ( )
        {
          clrscr ( );
          B b1;
         getch ( );
       }

   Output :

                      x = 10  y =20  z =30
                      a = 40   b =50  c =60 
Destructor and Inheritance  : Like a constructor function a base class and derived class can contain a destructor function . when an object of the derived class is created then the derived class constructor invokes the base class constructor is executed and then the derived class constructor is executed . The destructor functions are executed in the reverse of the order inwhich the constructor function were executed . Hence when a derived class destructor function is called , then it perform it's functionality and then it call the base class destructor function . At last the object are removed or destroyed from the memory of the computer. Also in multiple inheritance the constructor will be called in the order of inheritance . If any base class is derived as virtual base class then the constructor function of virtual base class is called before the non virtual function.


Use of Constructor and destructor in Inheritance
Class A
        {
           Public :
                    A ( )
                   {
                      x++ ;
                      cout <<" x = " << x <<"\n\n";
                    }
                      ~ A
                      {
                        x--;
                        cout << " x = "<<x" <<"\n\n";
                      }
              };
          Class B : public A
          {
            Public :
                      B ( )
                     {
                       x = x+10 ;
                       cout << "x="<<x<<"\n\n";
                     }
                      ~ B ( )
                        {
                            x = x - 10 ;
                           cout << "x ="<< x<<"\n\n";
                       }
            };
            Void main  ( )
            {
               clrscr ( );
              {
                cout <<" ===========\n\n";
                B b1  ,b2 , b3  ;
                cout <<" ===========\n\n";
              }                  
                          // www.programmingking.in

               cout <<" ===========\n\n";
               getch ( );
           }  
Passing parameters to base class constructors
By using a derived class constructor the parameter can be passed to the base class constructor . when a derived class constructor contain a list of parameters , then these parameters are passed to the base class constructor function . when an object is created  , of derived class then the constructor of base class initialized the object and invoked itself and then the constructor of derived class is invoked .
          Class A 
         {
           Public :
                     Int a , b ;
                     A ( int i , int j )
                     {
                         a =i ; b = j;
                        cout << "a ="<<a<<\t  b="<<b<<"\n\n";
                     }
         };
          Class B : Public A
          {
            Public :
                       Int c , d ;
                       B ( int x , int y , int z , int w ):A(x ,y)
                       {
                           c = z , d =w ;
                           cout << "c ="<<c<<"\t d ="<<d<<"\n\n";
                       }
          };
            Void main ( )
           {
              clrscr ( );
              cout <<"==========\n\n";
              B b1 ( 10 , 20 , 30 , 40 ) ;
              cout <<"=============\n\n";
              getch ( );
           }
               Output :
            ============
             a = 10  b =20
              c =30   d =40
           ============
Granting Access : c++ provides two ways for granting access of data and member functions .
  •  By using friend function : When we declare a function to any class as a friend function then this function can access the private and protected data member of the class . i.e , we are granting this friend function to have access the member of class . Also we can define a class as a friend class of another base class , this friend class can access the protected and private members of the another class.
  • By using Access specifier : There are three members to access the members:
  1. Private : By the use of private specifier access is granted only to the member functions  , friends and friend classes of the class .
  2. Protected : The protected members can be access by the member function of the class and by the member function of the derived class .
  3. Public : By using public specifier , access is granted to every members function inside the class or outside the class.

0 comments:

Post a Comment

 

PROGRAMMINGqueen Copyright © 2011 - |- Template created by O Pregador - |- Powered by Blogger Templates

Blogger Widgets