C++ Inheritance

In this C++ tutorial, let us have a look at inheritance, types of inheritance and sample C++ Inheritance program for better understanding the concepts.

Introduction of  C++ Inheritance 

It is one of the pillars of OOPs. It allows one class to inherit the features and behaviours of another class. The class which inherits the features is known as derived class or subclass. The class whose features are inherited is known as a base class or superclass. 

The derived class may also add new features and behaviours to the existing class. That does not make any change in the base class. The base class is the generalized class, whereas the derived class is a specialized one.

Types of Inheritance

The inheritance has been categorized based on whether a class inherits single class, multiple class, etc.

Different types of inheritance identified are as follows:

  • Single Inheritance
  • Multilevel Inheritance
  • Multiple Inheritance
  • Hybrid Inheritance
  • Hierarchical Inheritance

Single Inheritance

A single class when inherited by a single subclass is known as single inheritance.

Program for single inheritance in C++

#include <iostream> 
using namespace std; 
class base //single base class
{
   public:
     int x;
   void getdata()
   {
     cout << "Enter the value of x = "; cin >> x;
   }
 };
class derive : public base //single derived class
{
   private:
    int y;
   public:
   void readdata()
   {
     cout << "Enter the value of y = "; cin >> y;
   }
   void product()
   {
     cout << "Product = " << x * y;
   }
 }; 
 int main()
 {
    derive a; //object of derived class
    a.getdata();
    a.readdata();
    a.product();
    return 0;
 }    

Output

Enter the value of x = 3
Enter the value of y = 4
Product = 12

Multilevel Inheritance

If a class is derived from another derived class, then it is called Multilevel inheritance. So, the multilevel inheritance in C++ has more than one parent class.

Program for multilevel inheritance in C++

#include <iostream>
using namespace std;
class base //single base class
{
  public:
  int x;
  void getdata()
  {
     cout << "Enter value of x= "; cin >> x;
  }
};
class derive1 : public base // derived class from base class
{
  public:
  int y;
  void readdata()
  {
      cout << "\nEnter value of y= "; cin >> y;
  }
};
class derive2 : public derive1 // derived from class derive1
{
  private:
  int z;
  public:
  void indata()
  {
     cout << "\nEnter value of z= "; cin >> z;
  }
  void product()
  {
      cout << "\nProduct= " << x * y * z;
  }
};
int main()
{
     derive2 a; //object of derived class
     a.getdata();
     a.readdata();
     a.indata();
     a.product();
     return 0;
}          

Output

Enter value of x= 2
Enter value of y= 3
Enter value of z= 3
Product= 18

Hierarchical Inheritance

When several classes are derived from the common base class, then it is called Hierarchical inheritance.

Program for hierarchical inheritance in C++

#include <iostream> 
using namespace std;
class A //single base class
{
    public:
  int x, y;
  void getdata()
  {
        cout << "\nEnter value of x and y:\n"; cin >> x >> y;
  }
};
class B : public A //B is derived from class base
{
    public:
  void product()
  {
      cout << "\nProduct= " << x * y;
  }
};
class C : public A //C is also derived from class base
{
    public:
  void sum()
  {
        cout << "\nSum= " << x + y;
  }
};
int main()
{
    B obj1; //object of derived class B
    C obj2; //object of derived class C
    obj1.getdata();
    obj1.product();
    obj2.getdata();
    obj2.sum();
    return 0;
} 

Output

Enter value of x and y: 3
Product= 6
Enter value of x and y: 3
Sum= 5

Multiple Inheritance

If a class is derived from two or more base classes then it is called multiple inheritance.

Program for multiple inheritance in C++

#include<iostream.h> 
using namespace std;
class A
{
  public:
  int x;
  void getx()
    {
      cout << "enter value of x: "; cin >> x;
    }
};
class B
{
  public:
  int y;
  void gety()
  {
      cout << "enter value of y: "; cin >> y;
  }
};
class C : public A, public B //C is derived from class A and class B
{
  public:
  void sum()
  {
      cout << "Sum = " << x + y;
  }
};
int main()
{
   C obj1; //object of derived class C
   obj1.getx();
   obj1.gety();
   obj1.sum();
   return 0;

Output

enter value of x: 5
enter value of y: 4
Sum = 9

Hybrid Inheritance

Basically, it is a combination of one or more types, that in which the derivation of a class involves more than one form of any inheritance is called hybrid inheritance.

Program for hybrid inheritance in C++

#include <iostream>
using namespace std;
class A
{
  public:
  int x;
};
class B : public A
{
  public:
  B() //constructor to initialize x in base class A
  {
     x = 10;
  }
};
class C
 {
  public:
  int y;
  C() //constructor to initialize y
  {
      y = 4;
        }
};
class D : public B, public C //D is derived from class B and class C
{
  public:
  void sum()
  {
      cout << "Sum= " << x + y;
  }
};
int main()
{
         D obj1; //object of derived class D
  obj1.sum();
  return 0;
}                

Output

Sum= 14

Leave a Comment

Your email address will not be published. Required fields are marked *