Constructors and Destructors in Single and Multiple inheritance in C++

Constructors and Destructors in Multiple inheritances:

Constructors:- In multiple inheritances, the constructors of the base classes and constructors of the derived class are automatically executed when an object of the derived class is created. The constructors of the base classes are executed first and then the constructor of the derived class is executed. The constructors of base classes are executed in the same order in which the base classes are specified in derived class(i.e. from left to right).

Similarly, the destructors are executed in reverse order, i.e. derived class constructor is executed first and then constructors of the base classes are executed (from right to left).

If the constructors of the base classes have no parameters, then the syntax to create relationship between derived class and base classes is very simple.



Programming example:

Following the program example, explain the constructors and destructors in multiple inheritances

constructors

In this program, two classes base1 and base2 are defined. The class derive1 is publicly derived from two classes base1 and base2. It means that classes base1 and base2 are base classes while derive1 is a derived class. in the derived class “derive1”, the base1 is written first and then class base2 is written, the constructors of these classes are executed first in the same order as specified in the derived class (i.e. left t right) and then the constructor of derive1 class is executed. Similarly, destructors of these classes are executed in reverse order such as:

  • Destructor of derive1 class is executed first.
  • Destructor of base2 is executed after destructor of derived1.
  • Destructor of base1 is executed after destructor of base2.

Constructors in multiple inheritances with arguments:

The constructor of the derived class calls the constructors of the base classes in the same order in which they are specified in the header of the derived class. the syntax t define constructors in multiple inheritance with arguments is also similar to the definition of single inheritance with arguments. The constructors of the base classes are connected with the constructor of the derived class by using colon( : ) and separated by commas.


Programming example :

The following program example explains the constructors with arguments in multiple inheritances:

constructors

In this program the derived class derive1 contains four arguments, first two arguments for base1 class and third for base2 class. the fourth argument has its own argument.

Constructors and Destructors in Single Inheritance:

When an object of the derived class is created, a part of the base class is also included with that object. Actually, the base class is initiated to include the base class part with the object of the derived class and then the derived class part is included. So, when the derived class object is created, the constructor of the base class is automatically executed first and then the constructor of the derived class is executed.

The base class constructors are not inheritance by a derived class. however, a derived class constructor always calls the constructor for its base class first to initialize base class data members for the objects of the derived class. if the derived class constructor is omitted, the derived class default constructor calls the base class constructor.

Destructors are called in the reverse order of constructor calls. So a derived class destructor is called its base class destructor.

Programming example:

The following program example explains the concept of constructor and destructor in single inheritance:

constructors

In this program, both constructors of the base class and derived class do not use arguments. If the base class constructor uses no argument, then the syntax to create the relationship between derived class base is very simple.

When the object obj of class B is created during program execution, then the constructor of the base class is executed first and then the constructor of the derived class is executed, similarly, when the object of class B is destroyed (just before the program termination), the destructor of the derived class B is executed first and then destructor of the base class A is executed.


Constructor in Single Inheritance With Arguments:

the derived class is responsible for calling the constructor of the base class. in the case of the constructor of the base class with arguments, the syntax to define constructor of the derived class is different. The base class constructor is connected with the derived class constructor by using the colon (: ) in the header of the derived class constructor. The parameters of the base class constructor are also given as parameter list in the derived class constructor. In the derived class constructor, the parameters of the base class come first then the derived class constructor. The reason is that all data of the base class constructor is initialized before the derived class constructor.

Programming example:

The following program example explains the concept of a constructor with arguments in single inheritance:

constructors

In this program, the object obj of the derived class used four values. These values will be passed to the constructor of the derived class B, which used four parameters. The first two parameters ‘m’ and ‘n’ are used for base class constructor. The arguments ‘a’ and  ‘b’ are used for derived class constructor.



Related Article:

Pointer Arithmetic in C++ With Programming Examples

Recommended For You

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

Leave a Reply

%d bloggers like this: