Understanding Inheritance and Its Types In C++

Inheritance Definition

Inheritance is the concept in which a class derives the characters of another class similar to a child deriving characters from his/her parents.

The new class so formed is called the derived class or child class and the old class from which the characters are derived is the base class or parent class. The derived class derives the characters of the base class using ":" operator

  • Inheritance helps in code reusability. This not only saves time but also the reliability of the program.
  • Inheritance also reduces the complexity of a program.

How inheritance works in C++

In the below diagram, Class B derives all characteristics of class A. Class B may also have some additional characteristics in addition to the derived characteristics. 

Inheritance C++ Base Diagram

Consider the characteristics of a man and a woman. Both have a name, an age, height, and weight. But they also have some characteristics which are different from each other. While trying to implement the same in a program like saving the characteristics of a man and a woman, Inheritance can be used as below :

class Characters {
   String name;
   int age;
   float height;
   float weight;
}

This is the base class. Separate classes can be written for the other classes based on  the usage like,

class Man : public Characters {

}

and

class Woman  : public Characters{

}

Here both classes  Man and Woman inherits/derives all the characteristics of the class Characters, thus Inheritance reuses and reduces the code.

Inheritance In C++

Inheritance Types

  • Single Inheritance
  • Multiple Inheritance
  • Hierarchical Inheritance
  • Multilevel Inheritance
  • Hybrid Inheritance/Virtual Inheritance

Inheritance Example Programs in C++