Hybrid Inheritance in C++

Inheritance is the process by which objects of one class acquire the properties of another class. By this we can add additional features to an existing class without modifying it.
This is possible by deriving a new class from the existing one. The new class will have combined features of both the classes. In this tutorial we will study and understand the concept of  Hybrid Inheritance in C++

Hybrid Inheritance in C++

Hybrid Inheritance is the combination of two or more inheritances : single, multiple,multilevel or hierarchical Inheritances.

Hybrid Inheritance in C++

Hybrid Inheritance in C++ Example Program

Run Online

#include<iostream>
using namespace std;
int a,b,c,d,e;
class A    
{
protected:
public:
	void getab()    
	{
	cout<<"\nEnter a and b value:";
	cin>>a>>b;        
	}
};
 
class B:public A    {
protected:
public:
void getc()    
	{
	cout<<"Enter c value:";
	cin>>c;    
	}
};
 
class C    
{
protected:
public:
	void getd()    
	{
	cout<<"Enter d value:";
	cin>>d;    
	}
};
 
class D:public B,public C    
{
protected:
public:
	void result()    
	{
	getab();    getc();
	getd();    e=a+b+c+d;
	cout<<"\n Addition is :"<<e; 
	}
};
 
int main()    
{
	D d1;
	d1.result();
	
	return 0;
}
Output
Total Objects: 1
Enter a and b value: 5 10
Enter c value: 15
Enter d value: 20

Addition is :50

Watch it on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *