Close
Close

Multiple Inheritance in C++


Inheritance can be done in a number of ways. Till now, we have come across different types of inheritances in different examples.

The different types of inheritances which we have come across are:

Single Inheritance

single inheritance in C++

In single inheritance, a class inherits another class.

Multilevel Inheritance

multiple inheritance in C++

In this type of inheritance, one class inherits from another class. This base class inherits from some other class.

Hierarchical Inheritance

hierarchial in C++

In hierarchical inheritance, more than one class inherit from a base class.

Multiple Inheritance

multiple in C++

In this chapter, we will be studying about multiple inheritance.

In multiple inheritance, a class can inherit from more than one classes. In simple words, a class can have more than one parent classes. This type of inheritance is not present in Java.

Suppose we have to make two classes A and B as the parent classes of class C, then we have to define class C as follows.

class C: public A, public B
{
        // code
};

Let's see an example of multiple inheritance

#include <iostream>

using namespace std;

class Area
{
	public:
		int getArea(int l, int b)
		{
			return l * b;
		}
};

class Perimeter
{
	public:
		int getPerimeter(int l, int b)
		{
			return 2*(l + b);
		}
};

class Rectangle : public Area, public Perimeter
{
	int length;
	int breadth;
	public:
		Rectangle()
		{
			length = 7;
			breadth = 4;
		}
		int area()
		{
			return Area::getArea(length, breadth);
		}
		int perimeter()
		{
			return Perimeter::getPerimeter(length, breadth);
		}
};

int main()
{
	Rectangle rt;
	cout << "Area : " << rt.area() << endl;
	cout << "Perimeter : " << rt.perimeter() << endl;
	return 0;
}
Output
Area : 28
Perimeter : 22

In this example, class Rectangle has two parent classes Area and Perimeter. Class 'Area' has a function getArea(int l, int b) which returns area. Class 'Perimeter' has a function getPerimeter(int l, int b) which returns the perimeter.
When we created the object 'rt' of class Rectangle, its constructor got called and assigned the values 7 and 4 to its data members length and breadth respectively. Then we called the function area() of the class Rectangle which returned getArea(length, breadth) of the class Area, thus calling the function getArea(int l, int b) and assigning the values 7 and 4 to l and b respectively. This function returned the area of the rectangle of length 7 and breadth 4.
Similarly, we returned the perimeter of the rectangle by the class Perimeter.

Let's see one more example.

#include <iostream>

using namespace std;

class P1
{
	public:
		P1()
		{
			cout << "Constructor of P1" << endl;
		}
};

class P2
{
	public:
		P2()
		{
			cout << "Constructor of P2" << endl;
		}
};

class A : public P2, public P1
{
	public:
		A()
		{
			cout << "Constructor of A" << endl;
		}
};

int main()
{
	A a;
	return 0;
}
Output
Constructor of P2
Constructor of P1
Constructor of A

Here, when we created the object 'a' of class 'A', its constructor got called. As seen before, the compiler first calls the constructor of the parent class. Since class 'A' has two parent classes 'P1' and 'P2', so the constructors of both these classes will be called before executing the body of the constructor of 'A'. The order in which the constructors of the two parent classes are called depends on the following code.

class A : public P2, public P1

The order in which the constructors are called depends on the order in which their respective classes are inherited. Since we wrote 'public P2' before 'public P1', therefore the constructor of P2 will be called before that of P1.

To learn from simple videos, you can always look at our C++ video course on CodesDope Pro. It has over 750 practice questions and over 200 solved examples.

Words may lie but actions will always tell the truth.


Ask Yours
Post Yours
Doubt? Ask question