Multiple Inheritance is a feature of C++ where a class can inherit from more than one base class. It allows a derived class to combine properties and behavior from multiple parent classes, making code more reusable and flexible.
A class can be derived from more than one base class. For example:
- A CHILD class is derived from the FATHER and MOTHER class
- A PETROL class is derived from the LIQUID and FUEL class.
Syntax:
class A{
// Base class A };
class B{
// Base class B };
class C : public A, public B{
// Derived class };
Example: The following example demonstrates how a class can inherit from multiple base classes:
#include <iostream>
using namespace std;
class A {
public:
A() { cout << "Class A\n"; }
};
class B {
public:
B() { cout << "Class B\n"; }
};
// Inheriting from both class A & B
class C : public B, public A {
public:
C() { cout << "Class C\n"; }
};
int main() {
C obj;
}
Output
Class B Class A Class C
In the above code, class C inherits from both B and A using multiple inheritance (class C : public B, public A).
Constructor Call Order
In multiple inheritance, constructors of base classes are called in the same order in which they are inherited. This order is determined by the inheritance list, not by the order inside the constructor.
#include <iostream>
using namespace std;
class A {
public:
A() { cout << "Constructor A\n"; }
};
class B {
public:
B() { cout << "Constructor B\n"; }
};
class C : public B, public A {
public:
C() { cout << "Constructor C\n"; }
};
int main() {
C obj;
}
Output
Constructor B Constructor A Constructor C
This shows that constructors follow the inheritance order (B before A).
Destructor Call Order
Destructors are called in the reverse order of constructors. First, the destructor of the derived class is called, followed by base class destructors in reverse inheritance order.
#include <iostream>
using namespace std;
class A {
public:
~A() { cout << "Destructor A\n"; }
};
class B {
public:
~B() { cout << "Destructor B\n"; }
};
class C : public B, public A {
public:
~C() { cout << "Destructor C\n"; }
};
int main() {
C obj;
}
Output
Destructor C Destructor A Destructor B
This confirms that destructors execute in reverse order.
Diamond Problem
The Diamond Problem occurs when two classes inherit from the same base class, and a third class inherits from both of them. This leads to duplication of base class members and causes ambiguity.

In this example, we demonstrate the diamond problem where the base class constructor is called twice due to multiple inheritance.
#include <iostream>
using namespace std;
class Person {
public:
Person() {
cout << "Person constructor\n";
}
};
class Faculty : public Person {};
class Student : public Person {};
class TA : public Faculty, public Student {};
int main() {
TA obj;
}
Output
Person constructor Person constructor
Here, the constructor of Person is called twice, which means two copies of Person exist in TA. This creates ambiguity.
To solve the diamond problem, the virtual keyword is used while inheriting base classes. This ensures that only one shared copy of the base class is created, avoiding ambiguity. To understand this concept in detail, refer to Virtual Inheritancine C++.