Wednesday, May 21, 2014

Hybrid Inheritance in c++

Hybrid inheritance is combination of two or more inheritances such as single,multiple,multilevel or Hierarchical inheritances.
In the below program class B inherits property(member function) of class A which is base class and class D inherits property from class B and class C.

#include<iostream.h>
#include<conio.h>
 
class A     //Base class
{
public:
int l;
void len()
{
cout<<"\n\nLenght :::\t";  
cin>>l;                  //Lenght is enter by user      
}
};
class B :public A   //Inherits property of class A
{
public:
int b,c;
void l_into_b()  
{
len();
cout<<"\n\nBreadth :::\t";
cin>>b;                      //Breadth is enter by user
c=b*l;                       //c stores value of lenght * Breadth i.e. (l*b) .  
}
};
 
class C
{
public:
int h;
void height()
{
cout<<"\n\nHeight :::\t";
cin>>h;                  //Height is enter by user
}
};
 
//Hybrid Inheritance Level
class D:public B,public C
{
public:
int res;
void result()
{
l_into_b();
height();
res=h*c;                          //res stores value of c*h  where c=l*b and h is height which is enter by user
cout<<"\n\nResult (l*b*h) :::\t"<<res;
}
};
 
int main()
{
clrscr();
D d1;
d1.result();
getch();
}

Output_hybrid_inheritance

No comments:

Post a Comment