Multiple Inheritance in C++

Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes.

The constructors of inherited classes are called in the same order in which they are inherited. For example, in the following program, B’s constructor is called before A’s constructor.

filter_none

editclose

play_arrow

linkbrightness_4code

#include<iostream>

usingnamespacestd;

classA

{

public:

A() { cout << "A's constructor called"<< endl; }

};

classB

{

public:

B() { cout << "B's constructor called"<< endl; }

};

classC: publicB, publicA // Note the order

{

public:

C() { cout << "C's constructor called"<< endl; }

};

intmain()

{

C c;

return0;

}

chevron_right

filter_none

Output:

B's constructor called
A's constructor called
C's constructor called

The destructors are called in reverse order of constructors.

The diamond problem
The diamond problem occurs when two superclasses of a class have a common base class. For example, in the following diagram, the TA class gets two copies of all attributes of Person class, this causes ambiguities.

For example, consider the following program.

filter_none

editclose

play_arrow

linkbrightness_4code

#include<iostream>

usingnamespacestd;

classPerson {

// Data members of person

public:

Person(intx) { cout << "Person::Person(int ) called"<< endl; }

};

classFaculty : publicPerson {

// data members of Faculty

public:

Faculty(intx):Person(x) {

cout<<"Faculty::Faculty(int ) called"<< endl;

}

};

classStudent : publicPerson {

// data members of Student

public:

Student(intx):Person(x) {

cout<<"Student::Student(int ) called"<< endl;

}

};

classTA : publicFaculty, publicStudent {

public:

TA(intx):Student(x), Faculty(x) {

cout<<"TA::TA(int ) called"<< endl;

}

};

intmain() {

TA ta1(30);

}

chevron_right

filter_none

Person::Person(int ) called
Faculty::Faculty(int ) called
Person::Person(int ) called
Student::Student(int ) called
TA::TA(int ) called

In the above program, constructor of ‘Person’ is called two times. Destructor of ‘Person’ will also be called two times when object ‘ta1’ is destructed. So object ‘ta1’ has two copies of all members of ‘Person’, this causes ambiguities. The solution to this problem is ‘virtual’ keyword. We make the classes ‘Faculty’ and ‘Student’ as virtual base classes to avoid two copies of ‘Person’ in ‘TA’ class. For example, consider the following program.

filter_none

editclose

play_arrow

linkbrightness_4code

#include<iostream>

usingnamespacestd;

classPerson {

public:

Person(intx) { cout << "Person::Person(int ) called"<< endl; }

Person() { cout << "Person::Person() called"<< endl; }

};

classFaculty : virtualpublicPerson {

public:

Faculty(intx):Person(x) {

cout<<"Faculty::Faculty(int ) called"<< endl;

}

};

classStudent : virtualpublicPerson {

public:

Student(intx):Person(x) {

cout<<"Student::Student(int ) called"<< endl;

}

};

classTA : publicFaculty, publicStudent {

public:

TA(intx):Student(x), Faculty(x) {

cout<<"TA::TA(int ) called"<< endl;

}

};

intmain() {

TA ta1(30);

}

chevron_right

filter_none

Output:

Person::Person() called
Faculty::Faculty(int ) called
Student::Student(int ) called
TA::TA(int ) called

In the above program, constructor of ‘Person’ is called once. One important thing to note in the above output is, the default constructor of ‘Person’ is called. When we use ‘virtual’ keyword, the default constructor of grandparent class is called by default even if the parent classes explicitly call parameterized constructor.

How to call the parameterized constructor of the ‘Person’ class? The constructor has to be called in ‘TA’ class. For example, see the following program.

filter_none

editclose

play_arrow

linkbrightness_4code

#include<iostream>

usingnamespacestd;

classPerson {

public:

Person(intx) { cout << "Person::Person(int ) called"<< endl; }

Person() { cout << "Person::Person() called"<< endl; }

};

classFaculty : virtualpublicPerson {

public:

Faculty(intx):Person(x) {

cout<<"Faculty::Faculty(int ) called"<< endl;

}

};

classStudent : virtualpublicPerson {

public:

Student(intx):Person(x) {

cout<<"Student::Student(int ) called"<< endl;

}

};

classTA : publicFaculty, publicStudent {

public:

TA(intx):Student(x), Faculty(x), Person(x) {

cout<<"TA::TA(int ) called"<< endl;

}

};

intmain() {

TA ta1(30);

}

chevron_right

filter_none

Output:

Person::Person(int ) called
Faculty::Faculty(int ) called
Student::Student(int ) called
TA::TA(int ) called

In general, it is not allowed to call the grandparent’s constructor directly, it has to be called through parent class. It is allowed only when ‘virtual’ keyword is used.

As an exercise, predict the output of following programs.

Question 1

filter_none

editclose

play_arrow

linkbrightness_4code

#include<iostream>

usingnamespacestd;

classA

{

intx;

public:

voidsetX(inti) {x = i;}

voidprint() { cout << x; }

};

classB: publicA

{

public:

B() { setX(10); }

};

classC: publicA

{

public:

C() { setX(20); }

};

classD: publicB, publicC {

};

intmain()

{

D d;

d.print();

return0;

}

chevron_right

filter_none

Question 2

filter_none

editclose

play_arrow

linkbrightness_4code

#include<iostream>

usingnamespacestd;

classA

{

intx;

public:

A(inti) { x = i; }

voidprint() { cout << x; }

};

classB: virtualpublicA

{

public:

B():A(10) { }

};

classC: virtualpublicA

{

public:

C():A(10) { }

};

classD: publicB, publicC {

};

intmain()

{

D d;

d.print();

return0;

}

chevron_right

filter_none

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above