Page 207 - DCAP404 _Object Oriented Programming
P. 207

Object-oriented Programming




                    Notes          9.6 Constructors under Inheritance

                                   As we know, the constructors play an important role in initializing objects. We did not use them
                                   earlier in derived classes for the sake of simplicity. As long as no base class constructor takes any
                                   arguments, the derived class need not have a constructor function. However, if any base class
                                   contains a constructor with one or more arguments, then it is mandatory for the derived class to
                                   pass arguments to the base class constructor. When both the derived and base classes contain
                                   constructors, the base constructor is executed first and then the constructor in the derived class is
                                   executed.
                                   It case of multiple inheritance, the base classes are constructed in the order in which they appear
                                   in the declaration of the derived class. Similarly, in a multi-level inheritance, the constructors
                                   will be executed in the order of inheritance. Since the derived class takes the responsibility of
                                   supplying initial values to its base classes, we supply the initial values that are required by all
                                   the classes together when a derived class object is declared.
                                   The constructor of the derived class receives the entire list of values as its arguments and passes
                                   them on to the base constructors in the order in which they are declared in the derived class.

                                       !

                                     Caution  The base constructors are called and executed before executing the statements in
                                     the body of the derived constructor.
                                   Let us consider an example:

                                   Derived (int a1, int a2, float b1, float b2, int d1):
                                                 A(a1,  a2),  B  (b1,  b2)
                                          {
                                                 d  =  d1;
                                          }
                                   In the above constructor named derived, supplies the five arguments. The A(a1, a2) invokes the
                                   base constructor A( ) and B(b1, b2) invokes another base constructor B ( ). So the derived ( )
                                   constructor has one argument d1 of its own the derived constructor contains two parts separated
                                   by a colon (:). The first part provides the declaration of the arguments that are passed to the
                                   derived constructor and the second part lists the function calls to the base constructors.
                                   The constructor derived (.) may be invoked as follows:

                                          ……
                                          derived obj (10, 20, 15.0, 17.5, 40);
                                          ……

                                   The values are assigned to various parameters by the constructor derived ( ) as follows:
                                          a1 ? 10
                                          a2 ? 20
                                          b1 ? 15.0
                                          b2 ? 17.5

                                          d1 ? 40





          200                               LOVELY PROFESSIONAL UNIVERSITY
   202   203   204   205   206   207   208   209   210   211   212