Constructor in a derived class+Constructor invocation order in inheritance

The rule of constructor in derived class is as follows-:

“If there is no constructor in a base class then it is not necessary to have a constructor in the derived class too.

BUT if there is a  constructor in base class, then it is mandatory to have a constructor in  a derived class too”

Why?

because it is the job of a derived class constructor to pass value to base class constructor. (read this twice)

Now, you will see the constructor invocation order in inheritance.

Constructor invocation order in inheritance

In case of single inheritance, first constructor of base class is invoked and after that, constructor of derived class is invoked.

In case of multiple inheritance, the constructor invocation takes place according to the order of derived class declaration.

In case of multi level inheritance, the constructor invocation takes place according to the order of inheritance.

How to write this program-:
We will see a constructor invocation in a multiple inheritance…(multiple inheritance=multiple base classes and single derived class.)

Source:if you want to read about multiple inheritance again.

http://www.scitechtop.com/multiple-inheritance-in-c/

we make class alpha and beta and derive gamma from it. (alpha,beta and gamma are any assumptions; you can take any names)

in class alpha, we will make constructor alpha.

similarly in class beta, we will make a constructor beta.

and in class gamma, we will make a constructor gamma .

If you haven’t already properly studied about constructors, read about constructors here-:

http://www.scitechtop.com/write-a-c-program-showing-the-uses-of-constructorsdestructors-with-theory/

Output

alpha initialises

beta initialised

gamma intiialised

x=5

y=5.5

m=6

n=7

Leave a Comment

Your email address will not be published. Required fields are marked *

Do NOT follow this link or you will be banned from the site! Scroll to Top