Multiple Inheritance is one of the numerous features of the C++ language in which a class can inherit from more than one class. In doing this, the constructors of inherited classes are called in the same order in which they were inherited. Take for instance, in the program below, B’s constructor is called before A’s constructor.
Multiple Inheritance is a concept of inheritance, that allows a child class to inherit properties or behavior from multiple base classes. Hence, it can be said that it is the process that enables a derived class to acquire member functions, properties, and characteristics from more than one base class.
Check image:
Below are some of the features of Multiple Inheritance
Example:
#include <iostream>
class Area
{
public:
float area_calc(float l,float b)
{
return l*b;
}
};
class Perimeter
{
public:
float peri_calc(float l,float b)
{
return 2*(l+b);
}
};
/* Rectangle class is derived from classes Area and Perimeter. */
class Rectangle : private Area, private Perimeter
{
private:
float length, breadth;
public:
Rectangle() : length(0.0), breadth(0.0) { }
void get_data( )
{
cout<<"Enter length: ";
cin>>length;
cout<<"Enter breadth: ";
cin>>breadth;
}
float area_calc()
{
/* Calls area_calc() of class Area and returns it. */
return Area::area_calc(length,breadth);
}
float peri_calc()
{
/* Calls peri_calc() function of class Perimeter and returns it. */
return Perimeter::peri_calc(length,breadth);
}
};
void main()
{
Rectangle r;
r.get_data();
cout<<"Area = "<<r.area_calc();
cout<<"nPerimeter = "<<r.peri_calc();
getch();
}