Table of contents
1.
Introduction
2.
What is Multiple Inheritance?
3.
Syntax of Multiple Inheritance
4.
Examples of Multiple Inheritance
4.1.
Example 1
4.2.
C++
4.3.
Example 2 
4.4.
C++
4.5.
Example 3
4.6.
C++
5.
Working of Constructors & Destructors
5.1.
Example
5.2.
C++
6.
Ambiguity Problem or The diamond Problem
6.1.
C++
7.
Frequently Asked Questions
7.1.
What is the difference between multiple inheritance and single inheritance?
7.2.
Is multiple inheritance polymorphism?
7.3.
What languages support multiple inheritance?
7.4.
What is the Diamond Shape Problem in C++?
8.
Conclusion
Last Updated: Dec 17, 2024
Easy

Multiple Inheritance in C++

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In C++, multiple inheritance refers to the ability of a class to inherit from more than one base class. This feature allows a derived class to acquire the properties and behaviors of multiple parent classes, providing flexibility and reusability in object-oriented programming (OOP). Multiple inheritance can be particularly useful when a class needs to combine functionality from several classes without duplicating code. However, it also introduces complexities such as ambiguity and conflicts, which require careful handling. In this blog, we’ll explore how multiple inheritance works in C++, its advantages, challenges, and best practices for using it effectively.

Multiple Inheritance in C++

What is Multiple Inheritance?

Inheritance is the ability to inherit the features and functionalities from another class. It supports the reuse of the same code. Inheritance is the most important feature of object-oriented programming. A class which inherits the member functions and functionality from another class is called the derived class, and the class from which the derived class acquires is called the parent class. Multiple inheritance is a type of inheritance in C++ that allows a child to inherit properties and behaviour from multiple parent/base classes.

multiple inheritance

In the above example, there are two parent classes: Parent Class 1 and Parent Class 2, and there is only one derived class: Derived class. The derived class acquires all the features of Parent class 1 and Parent class 2.

Syntax of Multiple Inheritance

class X 
{
	// Member functions and data members for class X
};
class Y 
{
	// Member functions and data members for class Y
};
class Z : access modifier A, access modifier B 
{
	// Member functions and data members for class Z
};

Examples of Multiple Inheritance

Example 1

  • C++

C++

#include <bits/stdc++.h>  
using namespace std; 
class Add 

 public: 
 void sum(int x,int y) 

 cout << "The sum of " << x << " and " << y << " is " << x+y << endl; 

}; 

class Sub 

 public: 
 void sub(int x,int y) 

 cout << "The difference of " << x << " and " << y << " is " << x-y << endl; 

}; 

class derived: public Add, public Sub 

 // access specifier 
 public: 
derived(){ cout<<"Object of derived class created "<<endl; }
}; 

int main () 


derived dr;
dr.sum(10,1);
dr.sub(10,1);
You can also try this code with Online C++ Compiler
Run Code

Output

Object of derived class created 
The sum of 10 and 1 is 11
The difference of 10 and 1 is 9


Example 2 

  • C++

C++

#include <iostream>
using namespace std;
// Base class Animal
class Animal {
public:
void eat() {
cout << "Animal is eating." << endl;
}
};
// Base class Bird
class Bird {
public:
void fly() {
cout << "Bird is flying." << endl;
}
};
// Derived class Parrot
class Parrot : public Animal, public Bird {
public:
void speak() {
cout << "Parrot is speaking." << endl;
}
};
int main() {
Parrot parrot;
parrot.eat(); // From Animal class
parrot.fly(); // From Bird class
parrot.speak(); // Parrot's own function
return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output:

Animal is eating.
Bird is flying.
Parrot is speaking.

Example 3

  • C++

C++

#include <iostream>
using namespace std;

// Base class Vehicle
class Vehicle {
public:
void start() {
cout << "Vehicle is starting." << endl;
}
};

// Base class Engine
class Engine {
public:
void rev() {
cout << "Engine is revving." << endl;
}
};

// Derived class Car
class Car : public Vehicle, public Engine {
public:
void drive() {
cout << "Car is driving." << endl;
}
};

int main() {
Car car;
car.start(); // From Vehicle class
car.rev(); // From Engine class
car.drive(); // Car's own function

return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output:

Vehicle is starting.
Engine is revving.
Car is driving.

Working of Constructors & Destructors

Example

  • C++

C++

#include <bits/stdc++.h>
using namespace std;

class X{
 public:
X() { cout<<"Constructor of X is called"<<endl; }
~X() { cout<<"Destructor of X is called"<<endl; }
};

class Y{
 public:
Y() { cout<<"Constructor of Y is called"<<endl; }
~Y() { cout<<"Destructor of Y is called"<<endl; }
};

class Z:public X,public Y{
 public:
Z() { cout<<"Constructor of Z is called"<<endl; }
~Z() { cout<<"Destructor of Z is called"<<endl; }
};

int main(){
Z z;
}
You can also try this code with Online C++ Compiler
Run Code

Output

Constructor of X is called
Constructor of Y is called
Constructor of Z is called
Destructor of Z is called
Destructor of Y is called
Destructor of X is called

 

Explanation:

In case of multiple inheritance, the constructors of inherited classes are called in the exact same order in which they are inherited and the destructors are called in the reverse order of constructors.

Ambiguity Problem or The diamond Problem

The ambiguity problem arises when a single class is derived from two or more parent classes having the same-named member functions. The ambiguity is shown by the derived class object which invokes one of the same-named functions.

#include <bits/stdc++.h>
using namespace std;

class X{
    public:
        void display(){ cout<<"Display function of X"<<endl; }
};

class Y{
    public:
        void display(){ cout<<"Display function of Y"<<endl; }

};

class Z:public X,public Y{
    public:
        Z() { cout<<"Constructor of Z is called"<<endl; }
};

int main(){
    Z z;
    z.display();
}

 

Output

22:7: error: member 'display' found in multiple base classes of different types
    z.display();
      ^
6:14: note: member found by ambiguous name lookup
        void display(){ cout<<"Display function of X"<<endl; }
            ^
11:14: note: member found by ambiguous name lookup
        void display(){ cout<<"Display function of Y"<<endl; }

 

Inorder to resolve the ambiguity problem in multiple inheritance we use the resolution ‘::’ operator along with which we specify the class name from which the member function is to be invoked.

Syntax: 

objectDerivedClass.parentClassName::sameNamedFunction();

 

  • C++

C++

#include <bits/stdc++.h>
using namespace std;

class X{
 public:
 void display(){ cout<<"Display function of X"<<endl; }
};

class Y{
 public:
 void display(){ cout<<"Display function of Y"<<endl; }

};

class Z:public X,public Y{
 public:
Z() { cout<<"Constructor of Z is called"<<endl; }
};

int main(){
Z z;
z.X::display();
z.Y::display();
}
You can also try this code with Online C++ Compiler
Run Code

Output

Constructor of Z is called
Display function of X
Display function of Y

Frequently Asked Questions

What is the difference between multiple inheritance and single inheritance?

Multiple inheritance allows a class to inherit from more than one base class, enabling access to multiple sets of functionalities. In contrast, single inheritance restricts a class to inherit from only one base class, providing a simpler hierarchy.

Is multiple inheritance polymorphism?

Multiple inheritance is not polymorphism itself, but it can facilitate polymorphic behavior. Polymorphism allows methods to do different things based on the object that it is acting upon, and multiple inheritance provides multiple method implementations that can be accessed by a derived class.

What languages support multiple inheritance?

Languages that support multiple inheritance include C++, Python, and Ruby. However, some languages like Java and C# do not allow multiple inheritance directly but offer interfaces to achieve similar functionality without the complexities of multiple inheritance.

What is the Diamond Shape Problem in C++?

The diamond shape problem occurs in multiple inheritance when two base classes inherit from a common ancestor. A derived class inherits from both base classes, leading to ambiguity in accessing members of the common ancestor. This can be resolved using virtual inheritance.

Conclusion

In this blog, we discussed multiple inheritance in C++. We started by defining what multiple inheritance is and its importance. We then looked at how constructors and destructors work in multiple inheritance and addressed the ambiguity problem, along with ways to resolve it. Understanding these topics helps improve the use of multiple inheritance in C++ programming.

To detail the difference between procedural and object-oriented programming refer to this.

Live masterclass