Wednesday, May 21, 2014

Program for multilevel inheritance in c++

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().
#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 inheritance
class 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();
}

Multilevel Inheritance Output

No comments:

Post a Comment