Sunday, September 20, 2015

What is an inheritance? Write a program to describe the multilevel inheritance with suitable example.

Solution:-
Inheritance:-
        Inheritance is the process by which one object can acquire the properties of another object. It provides the concept of reusability.
Multi level inheritance:-
        A class can be derived from another derived class which is known as multilevel inheritance.
        Syntax:-
                   1st class name base class
                   2nd class name : 1st class name inheritance base class
                   3rd class name : 2nd class name derived class
Suitable Example:-
#include< iostream.h >
#include< conio.h >
class A
{
         public:
         int x ,y ;
};
class B : public A
{
         public:
         void getdata( )
         {
                    cout<< “ Enter the value of X : “;
                    cin>>x;
                    cout<< “ Enter the value of Y : “;
                    cin>>y;
         }
};
class C : public B
{
          int sum ;
          public:
          void getresult( )
          {   
                    getdata( );
                    sum=x+y ;
          }
          void display( )
          {
                   cout<< “sum x & b are: “<<sum<<endl ;
          }
};
void main( )
{
          C    NP , DK ;
          NP . getresult( );
          NP . display( );
          DK . getresult( );
          DK . display( );
          getch( );
}