Hierarchical inheritance
In these inheritance , derived class inherits property of Base Class but in these inheritance only one Base Class and multiple derived class are
involved. In the Below program Class A
is Base Class and Class B
,Class C ,Class D are derived class.(As Per below Diagram).
Example:
#include<iostream.h>
#include<conio.h>
class A
//Base Class
{
public:
int a,b;
void getnumber()
{
cout<<"\n\nEnter
Number :::\t";
cin>>a;
}
};
class B :
public
A //Derived Class 1
{
public:
void square()
{
getnumber(); //Call
Base class property
cout<<"\n\n\tSquare
of the number :::\t"<<(a*a);
cout<<"\n\n\t----------------------------------------------------";
}
};
class C :public A //Derived Class 2
{
public:
void cube()
{
getnumber(); //Call Base
class property
cout<<"\n\n\tCube
of the number :::\t"<<(a*a*a);
cout<<"\n\n\t----------------------------------------------------";
}
};
int main()
{
clrscr();
B
b1; //b1 is object of Derived
class 1
b1.square(); //call member function of class
B
C
c1; //c1 is object of Derived
class 2
c1.cube(); //call member
function of class C
getch();
}
OUTPUT
Multilevel Inheritance
When a class is derived from
another derived class is called multilevel inheritance. It is implemented by
defining at least three classes. In multilevel inheritance, there is one base
class and the remaining two is derived class.
In the below program class
bottom inherits property(member function) of class middle which is square() and
class middle inherits property of class
top which is getdata().
top which is getdata().
Example:
#include<iostream.h>#include<conio.h>class top
//base class{public :int a;void getdata(){cout<<"\n\nEnter first Number :::\t";cin>>a;}void putdata(){cout<<"\nFirst Number Is :::\t"<<a;}};//First level inheritanceclass middle :public top
// class middle is derived_1 {public:int b;void square(){getdata();b=a*a;cout<<"\n\nSquare Is :::"<<b;}};//Second level inheritance class bottom :public middle
// class bottom is derived_2 {public:int c;void cube(){square();c=b*a;cout<<"\n\nCube :::\t"<<c;}};int main(){clrscr();bottom b1;b1.cube();getch();} |
OUTPUT
Hybrid Inheritance
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.
Example:
#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:






No comments:
Post a Comment