VOOZH about

URL: https://www.geeksforgeeks.org/cpp/constructor-in-multiple-inheritance-in-cpp/

⇱ Constructor in Multiple Inheritance in C++ - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Constructor in Multiple Inheritance in C++

Last Updated : 23 Jul, 2022

Constructor is a class member function with the same name as the class. The main job of the constructor is to allocate memory for class objects. Constructor is automatically called when the object is created. 

Multiple Inheritance:

Multiple Inheritance is a feature of C++ where a class can derive from several(two or more) base classes. The constructors of inherited classes are called in the same order in which they are inherited.

👁 Image
Multiple Inheritance Model

Syntax of Multiple Inheritance:

👁 Image
Syntax of Multiple Inheritance:

class S: public A1, virtual A2
{
....
};                                                                                            

Here,
A2(): virtual base constructor
A1(): base constructor
S(): derived constructor

Example 1: Below is the C++ program to show the concept of Constructor in Multiple Inheritance.


Output
Constructor of the base class A2 
Constructor of the base class A1 
Constructor of the derived class S 

Time complexity: O(1)
Auxiliary Space: O(1)

Example 2: Below is the C++ program to show the concept of Constructor in Multiple Inheritance.


Output
Difference is:8
Sum is:55
Product is:320

Time complexity: O(1)
Auxiliary Space: O(1)

Comment