What is multiple inheritance?
- When a class is derived from another class ie it inherits functionalities of another class, this phenomenon is known as inheritance. In some cases, a class can inherit from multiple classes, ie a derived class can have multiple base classes, it is known as multiple inheritance.
- Consider the following example :
class Base1
{
private:
int no1;
public:
void show1()
{
cout << “The number is:” << no1 <<”\n”;
}
};
class Base2
{
private:
int no2;
public:
void show2()
{
cout << “The number is:” << no2 <<”\n”;
}
};
class Derived: public Base1, public Base2 //Multiple inheritance
{
public:
void set(int x, int y)
{
no1 = x; no2 = y;
}
};
int main()
{
Derived ob;
ob.set(10, 20);
ob.show1(); // Derived class obj can access member functions of both base classes
ob2.show2(); //Derived class obj can access member functions of both base class return 0;
}