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

C Program to Convert Decimal to Binary

In this tutorial, we will write a C program to convert decimal to binary. Before that, you must have knowledge of the following topics in …
Read More

C Program to Convert Binary to Decimal

In this tutorial, we will write a program to convert binary to decimal in C. Before that, you must have knowledge of the following topics …
Read More

C Program to Print ASCII Value of a Character

In this tutorial, we will write a program to print ASCII value of a character in C programming. Before that, you should have knowledge on …
Read More

Java Program to Convert Binary to Hexadecimal

In this tutorial, we will write a Java Program to Convert a Binary Number to Hexadecimal Number. Before that, you must have knowledge of the …
Read More

Java Program to Convert Hexadecimal to Binary

In this tutorial, we will write a Java Program to Convert a Hexadecimal Number to Binary Number. Before that, you must have knowledge of the …
Read More

Java Program to Convert Hexadecimal to Decimal

This is a Java Program to Convert a Hexadecimal Number to its decimal Equivalent. Before that, you must have knowledge of the following topics in …
Read More