Make Your Search Easy ! :) Use me

Showing posts with label Inheritance. Show all posts
Showing posts with label Inheritance. Show all posts

Friday, February 17, 2017

Hierarchical Inheritance


Program to demonstrate hierarchical Inheritance :-

#include<iostream>
#include<conio.h>


class base

{
   int p,q;
     public:
     void base_get()
      {
         cout<<"Enter Value of P :";
           cin>>p;
           cout<<"Enter Value of Q :";
           cin>>q;
        }
     void base_show()
      {
          cout<<"\nData of Base";
         cout<<"\nP = "<<p;
           cout<<"\nQ = "<<q;
        }

  };
class derived1:public base
{
   int r,s;
     public:
     void derived1_get()
      {
         cout<<"Enter Value of R :";
           cin>>r;
           cout<<"Enter Value of S :";
           cin>>s;
        }
     void derived1_show()
      {
         cout<<"\nData of Derived1";
           cout<<"\nR = "<<r;
           cout<<"\nS = "<<s;
        }
  };

class derived2:public base
{
   int a,b;
     public:
     void derived2_get()
      {
         cout<<"Enter Value of A :";
           cin>>a;
           cout<<"Enter Value of B :";
           cin>>b;
        }
     void derived2_show()
      {
         cout<<"\nData of Derived2";
           cout<<"\nA = "<<a;
           cout<<"\nB = "<<b;
        }
  };

void main()
{
   derived1 d1;
derived2 d2;
   d1.base_get();
   d1.derived1_get();
  d2.base_get();
   d2.derived2_get();
   clrscr();
   d1.base_show();
   d2.base_show();
   d1.derived1_show();
  d2.derived2_show();
   getch();
}

Multiple Inheritance

Program to demonstrate Multiple Inheritance :-

#include<iostream>
#include<conio.h>


class base1
{
   int p,q;
     public:
     void base1_get()
      {
         cout<<"Enter Value of P :";
           cin>>p;
           cout<<"Enter Value of Q :";
           cin>>q;
        }
     void base1_show()
      {
          cout<<"\nData of Base 1";
         cout<<"\nP = "<<p;
           cout<<"\nQ = "<<q;
        }


  };
class base2
{
   int r,s;
     public:
     void base2_get()
      {
         cout<<"Enter Value of R :";
           cin>>r;
           cout<<"Enter Value of S :";
           cin>>s;
        }
     void base2_show()
      {
         cout<<"\nData of Base 2";
           cout<<"\nR = "<<r;
           cout<<"\nS = "<<s;
        }
  };


class derived:public base1,public base2
{
   int a,b;
     public:
     void derived_get()
      {
         cout<<"Enter Value of A :";
           cin>>a;
           cout<<"Enter Value of B :";
           cin>>b;
        }
     void derived_show()
      {
         cout<<"\nData of Derived";
           cout<<"\nA = "<<a;
           cout<<"\nB = "<<b;
        }
  };


void main()
{
   derived d;
     d.base1_get();
     d.base2_get();
     d.derived_get();
     clrscr();
     d.base1_show();
     d.base2_show();
     d.derived_show();
     getch();


  }

Single Level Inheritance

Program to demonstrate Single Level Inheritance :-

#include<iostream.h>
#include<conio.h>


class base
{
int a,b;
public:
void base_get()
{
cout<<"\nEnter Value of A: ";
cin>>a;
cout<<"\nEnter Value of B: ";
cin>>b;
}
void base_show()
{
         cout<<"\nBASE CLASS DATA ";
cout<<"\nA = "<<a;
cout<<"\nB = "<<b;
}
};


class derived:public base
{
int p,q;
public:
void derived_get()
{
cout<<"\nEnter Value of P:  ";
cin>>p;
cout<<"\nEnter Value of Q:  ";
cin>>q;
}
void derived_show()
{
cout<<"\nDERIVED CLASS DATA ";
cout<<"\nP=  :"<<p;
cout<<"\nQ=  :"<<q;
}
   };


void main()
{
derived d;
d.base_get();
d.derived_get();
d.base_show();
d.derived_show();
getch();


}