The combination of the different types of inheritance such as multiple, simple, and hierarchical inheritance is known as Hybrid inheritance.
We already learned that in simple inheritance, one class is derived from a single class which is its base; in multiple inheritances, a class is derived from two classes, where one of the parents is also a derived class; in hierarchical inheritance, more than one derived class is created from a single base class.
However, hybrid inheritance is the combination of one or more inheritance types. Take, for example, the combination of single and hierarchical inheritance, as such hybrid inheritance can also be referred to as multipath inheritance.
So in addition, hybrid inheritance is the simple combination of two or more inheritances e.g single, multiple, multilevel or Hierarchical inheritances.
Example:
#include<iostream.h>
#include<conio.h>
class arithmetic
{
protected:
int num1, num2;
public:
void getdata()
{
cout<<"For Addition:";
cout<<"nEnter the first number: ";
cin>>num1;
cout<<"nEnter the second number: ";
cin>>num2;
}
};
class plus:public arithmetic
{
protected:
int sum;
public:
void add()
{
sum=num1+num2;
}
};
class minus
{
protected:
int n1,n2,diff;
public:
void sub()
{
cout<<"nFor Subtraction:";
cout<<"nEnter the first number: ";
cin>>n1;
cout<<"nEnter the second number: ";
cin>>n2;
diff=n1-n2;
}
};
class result:public plus, public minus
{
public:
void display()
{
cout<<"nSum of "<<num1<<" and "<<num2<<"= "<<sum;
cout<<"nDifference of "<<n1<<" and "<<n2<<"= "<<diff;
}
};
void main()
{
clrscr();
result z;
z.getdata();
z.add();
z.sub();
z.display();
getch();
}