Inheritance in C++6 min read

Inheritance is one of the most important concepts of object-oriented programming. In C++, inheritance is the process of deriving the properties and behaviors of one class to another class. It is like a child and parent where the child possesses the properties of the parent.

  • The class which inherits the members of another class is called the derived class (child/sub).
  • The class whose members are inherited is called the base class (super/parent).

Importance of Inheritance:

  • For Code Reusability.
  • For Method Overriding (to achieve runtime polymorphism).

A basic example of Inheritance.

As you can see in the above example Cat class inherits from the Animal class which means that the Cat class has can access the eat() and sleep() methods of Animal and also has its own additional method sound().

Modes of Inheritance.

It is the visibility mode that is used while deriving a parent class. such as:

We can derive the parent class publicly or privately.

Public mode: When the derive class publicly inherits the base class then the public members of the base class also become the public members of the derived class. Therefore, the public members of the base class are accessible by the objects of the derived class as well as by the member functions of the base class.

Private mode: When the derive class privately inherits the base class then the public members of the base class become the private members of the derived class.


Types Of Inheritance

C++ supports the following five types of inheritance.

  1. Single inheritance
  2. Multiple inheritance
  3. Multilevel inheritance
  4. Hierarchical inheritance
  5. Hybrid inheritance

1. Single Inheritance

It refers to an inheritance where a child inherits a single-parent class.
In the diagram below, class B inherits class A.

single Inheritance

Syntax:

Example: C++ example for single inheritance.

Output:

10
Result of addition: 30


2. Multiple Inheritance

Multiple inheritance is the process where the single class inherits the properties from two or more classes.

In the diagram below, class C inherits from both Class A and Class B.

multiple Inheritance

Syntax:

Example: C++ example for multiple inheritance.

Output:

num1 : 5
num2 : 10
Result of sum: 15


3. Multilevel Inheritance

In this type inheritance derived class is created from another derived class. In multilevel inheritance, one class inherits from another class which is further inherited by another class. The last derived class possesses all the members of the above base classes.

Multilevel Inheritance

Example: C++ example for multilevel inheritance.

Output:

Animal sleep
Dog Barking
Puppies


4. Hierarchical Inheritance

The process of deriving more than one class from a base class is called hierarchical inheritance. In other words, we create more than one derived class from a single base class.

hierarchical Inheritance

Syntax:

Example: C++ example for hierarchical Inheritance.

Output:

Animal sleep
Meow

Animal sleep
Barks


5. Hybrid (Virtual) Inheritance

Hybrid inheritance is the combination of more than one type of inheritance. In the diagram below is the combination of hierarchical Inheritanceand multiple inheritance.

hybrid Inheritance

Example: C++ example for hierarchical Inheritance.

Output:

Value of a: 20
Value of b: 30
Value of c: 50
Sum : 100


Ambiguity in Inheritance

When there is a function present with the same name in two or more base classes then during multiple inheritances or single inheritance an ambiguity can occur in such cases.

Example: C++ example for ambiguity in multiple inheritance.

There are display() functions in both the classes of One and Two. The following error will be displayed after execution.

To overcome this problem we need to do the following changes in Class Three by using the class resolution operator (::).


MORE

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …