Program Demonstrates Constructor With No Parameter In Multiple Inheritance In C++

LEVEL: Beginner

Whats in this post?

  1. Code
  2. Output
  3. Background and Explanation of the code

Code


To copy code: Click View Source “View Source” on the right of this code snippet.


Background and Explanation of the code


The above output shows that when the object of the derived class d is created then before the execution of derived class constructor , the base classes constructor will be executed in the same order as defined in the derv class declaration.However if we change the derv class declaration by changing the order in which the base classes are specified as follows

class derv : public base2, public base1

then during the execution of the program we get the following output

Base2 no parameter constructor

Base1 no parameter constructor

Derv  no parameter constructor

The above output shows that class base2’s constructor is executed before the class base1’s constructor.thus it confirms that in case of multiple inheritance when the object of derived class is created ,the order in which the base class constructors are executed is determined by the order in which base classes are specified in the derived class declaration.

OUTPUT

Base1  no parameter constructor

Base2  no parameter constructor

Derv  no parameter constructor