In this C++ tutorial, you will learn what Multiple Inheritance is, how to define a child class that inherits from multiple classes, with examples.

C++ Multiple Inheritance

In C++, Multiple Inheritance is a scenario where a class inherits more than one class.

We will also discuss a scenario where there could be a situation when more than one base classes has a function with the same name.

Examples

1. Class B inherits both Class A and Class X

The following is a simple example where A and X are base classes and class B inherits both A and X.

C++ Program

</>
Copy
class A {
public:
    void funA() {}
};

class X {
public:
    void funX() {}
};

class B: public A, public X {
public:
    void funB() {
        funA();
        funX();
    }
};

Now, let us define some properties and functions in these classes and see how we can create an object of class B, and access the properties and functions of classes A and X.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

class A {
public:
    int a = 3;
    void funA() {
        cout << "Running function in Class A." << endl;
    }
};

class X {
public:
    int x = 9;
    void funX() {
        cout << "Running function in Class X." << endl;
    }
};

class B: public A, public X {
public:
    void funB() {
        cout << "Running function in Class B." << endl;
    }
};

int main() {
    B bObj = B();
    bObj.funB();
    bObj.funA();
    bObj.funX();
    cout << "Property of A : a = " << bObj.a << endl;
    cout << "Property of X : x = " << bObj.x << endl;
}

Output

Running function in Class B.
Running function in Class A.
Running function in Class X.
Property of A : a = 3
Property of X : x = 9
Program ended with exit code: 0

2. Parent Classes with a function of same name

In the following program, A and X are base classes and these classes have a method with the name fun().

Since B inherits both these classes A and X, if we try to access the function fun() from class B, or via B instance objects, the program does not compile.

We must specify the base class while calling the function as A::fun() or X::fun().

C++ Program

</>
Copy
#include <iostream>
using namespace std;

class A {
public:
    void fun() {
        cout << "Running function in Class A." << endl;
    }
};

class X {
public:
    void fun() {
        cout << "Running function in Class X." << endl;
    }
};

class B: public A, public X {
};

int main() {
    B bObj = B();
    bObj.A::fun();
    bObj.X::fun();
}

Output

Running function in Class A.
Running function in Class X.
Program ended with exit code: 0

Note

Also, note that there would no issue if there is also a function with the same name in derived class. A call with this function name, without any mention to which class, the function in derived class would be executed.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

class A {
public:
    void fun() {
        cout << "Running function in Class A." << endl;
    }
};

class X {
public:
    void fun() {
        cout << "Running function in Class X." << endl;
    }
};

class B: public A, public X {
public:
    void fun() {
        cout << "Running function in Class B." << endl;
    }
};

int main() {
    B bObj = B();
    bObj.fun();
}

Output

Running function in Class B.
Program ended with exit code: 0

Conclusion

In this C++ Tutorial, we learned what Inheritance is, how to use inheritance in C++, with the help of examples.