Multiple Inheritance in C++ (Inherit from Multiple Classes)

In this tutorial we will cover the concept of Multiple Inheritance in C++. The basic concept of Multiple Inheritance is to directly inherit more than just one Class at a time. This is a feature available in very languages such as C++.

There are several types of Inheritance in C++, such as Simple Inheritance, Multilevel Inheritance, Hierarchical Inheritance, and of course, Multiple Inheritance. It is worth noting, that Multiple Inheritance is not actually very popular, and used scarcely. Nonetheless,


Inheritance from Multiple Classes in C++

The basic concept is pretty straightforward, and simply requires you to mention two Classes for Inheritance after the Class name. You get to define a access modifier for each Class you want to inherit, individually.

class A
 {
public:
  A()  { cout << "Class A constructor called" << endl; }
};
  
class B
 {
public:
  B()  { cout << "Class B constructor called" << endl; }
};
  
class C: public B, public A  {
public:
  C()  { cout << "Class C constructor called" << endl; }
};
  
int main()
{
    C c;
    return 0;
}
B's constructor called
A's constructor called
C's constructor called

Notice the order in which the Constructors were called. First the Constructors of the Parent Classes are called, in the order that they were inherited. So Class B’s constructor is called first, then A. After that only, is the Constructor for Class C finally called.


Problems with Multiple Inheritance

A common problem that can arise especially during multiple inheritance, is the Diamond Inheritance Problem. Let’s take a look at an example that will highlight this issue.

#include <iostream>
using namespace std;

class A {
public:
  A()  { cout << "Class A constructor called" << endl; }
};

class B: public A {
public:
  B()  { cout << "Class B constructor called" << endl; }
};

class C: public A {
public:
  C()  { cout << "Class C constructor called" << endl; }
};

class D: public B, public C {
public:
  D()  { cout << "Class D constructor called" << endl; }
};

int main() {
    D d;
}
Class A constructor called
Class B constructor called
Class A constructor called
Class C constructor called
Class D constructor called

Looking at the output, we can see that Class A’s constructor was called twice. This means that two objects for Class A were created. The reason being, that Class B creates one object for Class A, and Class C does the same. Then when Class D inherits from both Class B and C, it ends up with two objects of Class A!

Now this introduces ambiguity into our code. When trying to access a variable or function from Class A in Class D, which one of the two objects is going to be used? And because this confusion and ambiguity exists, the compiler will simply throw an error.


Solving the Diamond Problem

Luckily, the fix for this is pretty simple. We just need to make use of the Virtual keyword! All we need to do, is when inheriting when we make class B and class C inherit from Class A, we add the virtual keyword after the colon in the class name.

What this does, is shifts the responsibility of initializing Class A, to Class D. Previously Class B and C were responsible for initializing it, but since both were doing it, things became ambiguous. So if only Class D initializes Class A, there won’t be any problems as only one object is created. (This is more noticeable when using parametrized constructors, which we have explained how to do here)

Let’s modify our code now to include the Virtual keyword.

#include <iostream>
using namespace std;

class A {
public:
  A()  { cout << "Class A constructor called" << endl; }
};

class B: virtual public A {
public:
  B()  { cout << "Class B constructor called" << endl; }
};

class C: virtual public A {
public:
  C()  { cout << "Class C constructor called" << endl; }
};

class D: public B, public C {
public:
  D()  { cout << "Class D constructor called" << endl; }
};

int main() {
    D d;
}
Class A constructor called
Class B constructor called
Class C constructor called
Class D constructor called

It works! Only one object for Class A was created this time.

For a more detailed outlook on the Diamond Inheritance Problem, please refer to it’s dedicated tutorial that we made.


This marks the end of the Multiple Inheritance in C++ Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments