Page 208 - DCAP404 _Object Oriented Programming
P. 208

Unit 9: Inheritance




          The constructors for virtual base classes are invoked before any non-virtual base classes. If there  Notes
          are multiple virtual base classes, they are invoked in the order in which they are declared. Any
          non-virtual bases are then constructed before the derived class constructor is executed.
                              Table  9.1: Execution  of base  Class  Constructors

            Methods of Inheritance               Order of execution
            Class X: public y { };               Y( ); base constructor
                                                 X ( ); derived constructor
            Class X: public y, public Z          Y( ); base constructor (first)
            { };                                 Z( ); base constructor (second)
                                                 X( ); derived constructor
            Class X: public Y, virtual public Z   Z( ); virtual base constructor
            { };                                 Y ( ); ordinary base constructor
                                                 X( ); derived constructor

          Let us consider a program using constructors:
          #  include  <iostream.h>

          class  A
          {
                 int  a1;
                 public:
                         A  (int  x)
                 {
                         a1  =  x:

                         cout  <<  “constructor  of  A\n”;
                 }
                 void  display  (void)
                 {
                 cout  <<  “a1  =  “  <<  a1  <<  “\n”;
                 }
          }
          class  B
          {

                 float  b1;
                 public:
                         B(  float  y)
                         {
                                b1  =  y;
                                cout  <<  “  constructor  of  B\n”;
                         }





                                           LOVELY PROFESSIONAL UNIVERSITY                                   201
   203   204   205   206   207   208   209   210   211   212   213