Inheritance is a mechanism in which one class can acquire properties of another class; thus helps in the reusability of existing code, inheritance is one of the main features in object-oriented programming.

All You Need to Know About Inheritance in C++

Base Class: The class whose properties are inherited is called a base class; it can also be termed as parent class.

Derived Class: The class that inherits the properties from a base class is called the derived class; it can also be termed as the derived class.

Inheritance is mainly used when a class requires some properties which another class already has, let’s see an example.

Suppose we want to have a class for car, bus, truck which will have properties like saying max speed, mileage, capacity. Now we need to have a class like.

Inheritance in C++

We can clearly see the repetition of properties in all the above 3 classes which results in duplication, in this scenario if we use inheritance we can avoid duplication and hence can use reusability.

Inheritance Programming

Here we are creating a single class called vehicle with properties and now other classes inherit from the vehicle class, vehicle class is the base class and car, bus, truck are derived classes, here we have to write the functions only one time and can be used by derived classes for its own specific tasks, please note that derived class has to implement its own functions with its specific implementation which is called function overriding.

Let’s see the syntax of inheritance in C++

class derived : access_specifier class base {
            // class members
}

Derived class and base class is self-explanatory, access specifier is the way the derived class inherits properties from the base class, and there are 3 types of access specifiers private, protected, public, these are used while deriving the class from the base class and also to define the scope of the member variables and functions inside the class.

Private access specifier: When we use a private access specifier during class derivation then public and protected members of the base class become private, and private members from the base class do not get inherited.

Protected access specifier: When we use a protected access specifier during class derivation then public and protected members of the base class become protected in the derived class.

Public access specifier: When we use public access specifier during class derivation then public members from the base class will be public in derived and protected will become protected in the derived class.

The below table summarises the results after deriving base class members using specific access specifiers.

Base class member types Public Inheritance Protected Inheritance Private inheritance
 

Public

 

Public

 

Protected

 

Private

 

Protected

 

Protected

 

Protected

 

Private

 

Private

cannot be inherited cannot be inherited cannot be inherited

 

Let’s see code example:

class Base {
   private:
      int a;
   protected:
      int b;
   public:
      int c;
}
class derived: public base {
   // a is not accessible
   protected:
      int b; // b becomes protected
   public:
      int c ; // c becomes public         
}
class derived: protected base {
    // a is not accessible
    protected:
      int b; // b becomes protected
      int c; // c becomes protected    
}

 

class derived: private base {
    // a is not accessible
    private:
      int b;  // becomes private
      int c;  // becomes private
}

 

Types of inheritance

Single inheritance

When a derived class inherits only from one base class it’s called single inheritance. The base class can also be called a parent and the derived class can be called a child class

Inheritance C++ Classes

 

class derived : access_specifier base{
  //class body
}

 

Multiple Inheritance: When a derived class has more than one direct base class then it’s called multiple inheritances. As we can see in the below example Derived class has two base classes.

Inheritance Classes

class derived : access_specifier base1, access_specifier base2 {
  //class body
}

Multi-Level Inheritance: When a derived class is created from another derived class, or we can say if a derived class has grandparents then it’s called multi-level inheritance. As we can see the derived class is derived from base class 2 and in turn it is derived from base class 1, here base class 1 can be called grandparent, and base class 2  is called as a parent of the derived class.

Inheritance for Programmers

 

class base2 : access_specifier base1 {
  //class body
}

 

class derived : access_specifier base2 {
  //class body
}

 

Hierarchical Inheritance: When more than one derived class is inherited from a single base class then it’s called hierarchical inheritance. As seen in the below example derived classes 1 and 2 have a single base class as common.

How to Code in C++

 

Hybrid Inheritance: When more than one type of inheritance mechanism is used it is called hybrid inheritance.

Inheritance Coding

 

Sample program to show inheritance:

#include<bits/stdc++.h>
using namespace std;
class base {
   public:
     int b;
};
class derived public base{
   public:
     int d;
};
void main(){
   derived obj;
   obj.b = 10;
   obj.d = 20;
   cout<<”Value of derived class member is “<<obj.d;
   cout<<”Value of base class member is “<<obj.b;
}

 

Diamond problem in inheritance: When a derived class has more than one base class and that base classes are derived from a common base class in this scenario problem of ambiguity arises Let’s see a typical scenario.

How to Program in C++

 

Seeing the above inheritance scenario, the salary class will receive the Name, Grade of employee 2 times once from the HR class and once from Development class which can result in ambiguity, to overcome such ambiguity we need to use a virtual mechanism, let’s see how to use it.

class Employee {
   public:
      string Name;
      int Grade;
}
class HR: virtual public Employee {
   //class body
}
class Development: virtual public Employee{
   //class body
}
class Salary :public HR, public Development{
   //class body
}


As you can see we have added a virtual keyword while deriving the HR, Development class, now the Salary class will inherit Name, Grade only once. Development and HR are called virtual base classes.