Object Inheritance C++ (Polymorphic classes)

Inheritance is the capability of a class to derive properties and characteristics from another class. It is considered one of the most vital concepts in object-oriented programming. The most common keywords used in inheritance are:

  • Subclass – Also known as the child class. It is the class that inherits properties, features, and behaviors of another class
  • Superclass – Also known as the parent class or base class. It is the class whose features, properties and behavior are inherited by the subclass.

When do we use inheritance?

Multiple classes may share similar features. Without inheritance, the codes may have duplication which causes data redundancy and chances of error. Take for example three classes of vehicles Bus, Car and Truck. All these classes have a FuelAmount, Capacity and Brakes. Without inheritance, we will have to repeat the three features in all the classes as shown below

Class Bus                 Class Truck                Class Car












We can use inheritance to avoid repetition and redundancy as shown below:

When we use inheritance we only have to write the features once for the parent class (vehicles) instead of three times. The rest of the sub-classes (Bus, Truck, and Car) will inherit the properties from the base class.

How Inheritance is implemented in C++

In C++ we use the syntax below to create a sub-class which inherits from the base class:

Class subclass_name :access_modebase_class_name


//code body of the subclass



  • Sub_class – The name of the sub_class
  • Access_mode –The mode you want to inherit this sub class for. For example, private, public, etc.
  • Base_class – The name of the base class from which the sub_class will be inherited from

Please note that a sub_class does not inherit access to private data members. However, it inherits a full parent object which has any private members that the class declares.

Code example

// C++ code that illustrates how inheritance is implemented

#include <bits/stdc++.h>

Using namespace std;

//parent class

Class parent





// A sub class that inherits from the parent class

Class child : public parent





//The main function

Int main ( )


Child obj1;

//object of class child that has all the data members and member functions of class parent

Obj1.id_c = 8

Obj1.id_p = 93

Cout<< “The child id is” << obj1.id_c <<endl;

Cout<< “The child id is” << obj1.id_c <<endl;

return 0;


The output from the program above will be:

The child id is 8

The child id is 93

In our example program, the child class inherits the public data members of the class parent.

Modes of inheritance in C++

  • Public mode

This is when we derive a sub class from a public parent class. The public members of the parent class will become public in the derived class (sub class). Also, the protected members of the super class will become protected in the derived class.

  • Protected mode

It is when we derive a sub-class from a protected parent class. Both the protected and public members of the super class becomes protected in the child class (derived class).

  • Private mode

This is when we derive a sub class from a private parent class. Both the protected and public members of the super class become private in the derived class.

Please note that you cannot directly access the members of the base class from the sub class. Only the protected members can be accessed directly.

Types of inheritance in C++

  • Single Inheritance

In a single type of inheritance, a sub-class can only inherit the properties of one parent class.

Class 1 (parent class)

Class 2 (sub class)

The syntax for a single inheritance is:

Class subclass_name :access_modebase_class




  • Multiple inheritances

In C++, the multiple inheritance feature is where a sub class can inherit the properties, behavior or features of more than one base classes.

The syntax for multiple inheritances is:

Class subclass_name :acces_modebase_class 1, access_modebase_class 2,




In multiple inheritances, the base classes are separated by a comma. Also, we must specify the access mode for every base class.

Other types of inheritances in C++ include

  • Multilevel inheritance
  • Hierarchical inheritance
  • Hybrid or virtual inheritance

We provide impeccable C++ programming assignment help on assignments related to inheritance, polymorphism and many more. Get in touch with us immediately if you want to submit error-free codes for your C++ assignment.