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++
#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++
#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++
#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++
#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++
#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.