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

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More

Inverted Half Pyramid Pattern of Alphabets in C

In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in …
Read More