C++ Inheritance

Inheritance


  • Capability of one class to inherit the properties of another class
  • Class whose properties are inherited is known as base class or parent class or super class
  • Class which derives properties of another class is known as child class, derived class or subordinate class.
  • Inheritance can express real world inheritance relationships.
  • Inheritance provides reusability of existing code.
  • Development time is automatically reduced.


Types of Inheritance


  1. Single Inheritance
  2. Multiple Inheritance
  3. Multilevel Inheritance
  4. Heirarchical Inheritance
  5. Hybrid Inheritance

Single Inheritance


There is only one parent class and one child class..

Program to show Single Inheritance



#include<iostream.h>
#include<conio.h>
class student()        //Parent Class
{
   private:
      int rollno;
      char name[20];
   public:
     void readstud()
     {
          cout<<"\nEnter Roll No = ";
          cin>>rollno;
          cout<<"\nEnter Name =";
          gets(name);
     } 
     void showstud()
     {
          cout<<"\nRoll No = "<<rollno;
          cout<<"\nName = "<<name;
     }
};
class data:public student                  // Child Class
{
      private:
          char address[50];
     public:
         void readaddr()
         {
             cout<<"\nEnter Address = ";
             gets(address);
         }
         void showaddr()
         {
               cout<<"\nAddress = "<<address;
         }
};
void main()
{
       clrscr();
       data d;
       d.readstud();
       d.readaddr();
       d.showstud();
       d.showaddr();
       getch();
}


Output

Enter Roll No =27
Enter Name = Rahul
Enter Address =Pathankot
Roll No = 27
Name = Rahul
Address = Pathankot

Multiple Inheritance


More than one parent class but only one child class. All the properties and behavior of multiple classes is inherited by one class.

Program to show Multiple Inheritance



#include<iostream.h>
#include<conio.h>
class student()        //Parent Class
{
   private:
      int rollno;
      char name[20];
   public:
     void readstud()
     {
          cout<<"\nEnter Roll No = ";
          cin>>rollno;
          cout<<"\nEnter Name =";
          gets(name);
     } 
     void showstud()
     {
          cout<<"\nRoll No = "<<rollno;
          cout<<"\nName = "<<name;
     }
};
class ages       //Parent Class
{
   private:
      int age;
   public:
     void readage()
     {
          cout<<"\nEnter Age = ";
          cin>>age;
     } 
     void showage()
     {
          cout<<"\nAge = "<<age;
     }
};
class data:public student, public ages                  // Child Class
{
      private:
          char address[50];
     public:
         void readaddr()
         {
             cout<<"\nEnter Address = ";
             gets(address);
         }
         void showaddr()
         {
               cout<<"\nAddress = "<<address;
         }
};
void main()
{
       clrscr();
       data d;
       d.readstud();
       d.readage();
       d.readaddr();
       d.showstud();
       d.showage();
       d.showaddr();
       getch();
}


Output

Enter Roll No =27
Enter Name = Rahul
Enter Age = 16
Enter Address =Pathankot
Roll No = 27
Name = Rahul
Age = 16
Address = Pathankot

Multilevel Inheritance


One class is parent of another.

Program to show Multilevel Inheritance



#include<iostream.h>
#include<conio.h>
class student()        //Parent Class
{
   private:
      int rollno;
      char name[20];
   public:
     void readstud()
     {
          cout<<"\nEnter Roll No = ";
          cin>>rollno;
          cout<<"\nEnter Name =";
          gets(name);
     } 
     void showstud()
     {
          cout<<"\nRoll No = "<<rollno;
          cout<<"\nName = "<<name;
     }
};
class ages: public student        //Child of Student, Parent of data Class
{
   private:
      int age;
   public:
     void readage()
     {
          cout<<"\nEnter Age = ";
          cin>>age;
     } 
     void showage()
     {
          cout<<"\nAge = "<<age;
     }
};
class data:public ages                  // Child Class
{
      private:
          char address[50];
     public:
         void readaddr()
         {
             cout<<"\nEnter Address = ";
             gets(address);
         }
         void showaddr()
         {
               cout<<"\nAddress = "<<address;
         }
};
void main()
{
       clrscr();
       data d;
       d.readstud();
       d.readage();
       d.readaddr();
       d.showstud();
       d.showage();
       d.showaddr();
       getch();
}


Output

Enter Roll No =27
Enter Name = Rahul
Enter Age = 16
Enter Address =Pathankot
Roll No = 27
Name = Rahul
Age = 16
Address = Pathankot

Hierarchical Inheritance


One Parent class but more than one child class. More than one class can inherit properties and behavior of one class.

Program to show Multilevel Inheritance



#include<iostream.h>
#include<conio.h>
class student()        //Parent Class
{
   private:
      int rollno;
      char name[20];
   public:
     void readstud()
     {
          cout<<"\nEnter Roll No = ";
          cin>>rollno;
          cout<<"\nEnter Name =";
          gets(name);
     } 
     void showstud()
     {
          cout<<"\nRoll No = "<<rollno;
          cout<<"\nName = "<<name;
     }
};
class ages: public student 
{
   private:
      int age;
   public:
     void readage()
     {
          cout<<"\nEnter Age = ";
          cin>>age;
     } 
     void showage()
     {
          cout<<"\nAge = "<<age;
     }
};
class data: public student
{
      private:
          char address[50];
     public:
         void readaddr()
         {
             cout<<"\nEnter Address = ";
             gets(address);
         }
         void showaddr()
         {
               cout<<"\nAddress = "<<address;
         }
};
void main()
{
       clrscr();
       data d;
       ages a;
       cout<<"\n For a =";
       a.readstud();
       a.readage();
       a.showstud();
       a.showage();
       cout<<"\n\n For d =";
       d.readstud();
       d.readaddr();
       d.showstud();
       d.showaddr();
       getch();
}


Output

For a=
Enter Roll No =27
Enter Name = Rahul
Enter Age = 16
Roll No = 27
Name = Rahul
Age = 16

For b=
Enter Roll No =27
Enter Name = Rahul
Enter Address =Pathankot
Roll No = 27
Name = Rahul
Address = Pathankot

Hybrid Inheritance


It is combination of all other types of inheritance. Multiple parents of a class may have same parent.

Program to show hybrid inheritance



#include<iostream.h>
#include<conio.h>
class top
{
   public:
   int a;
};
class base1:public top
{
   public:
   int b;
};
class base2:public top
{
   public:
   int c;
};
class child:public base1, public base2
{
   public:
   int d;
};
void main()
{
     clrscr();
     child ob1;
     ob1.base1::a=10;
     ob1.base2::a=20;
     ob1.b=5;
     ob1.c=7;
     ob1.d=3;
     cout<<"ob1.base1::a="<<ob1.base1::a;
     cout<<"ob1.base2::a="<<ob1.base2::a;
     cout<<"ob1.b="<<ob1.b;
     cout<<"ob1.c="<<ob1.c;
     cout<<"ob1.d="<<ob1.d;
     getch();
}



Output


ob1.base1::a=10
ob1.base2::a=20
ob1.b=5
ob1.c=7
ob1.=3

Back To Top