Home | Computer | Inheritance in C++ – Advantages and Categories

Inheritance in C++ – Advantages and Categories

November 8, 2022

Inheritance is a programming technique that allows you to reuse an existing class to create a new one. The new class inherits all the properties and methods of the original class. This is useful when you want to create a new class that shares some similarities with an existing class but also has some unique features.

The superclass, base class, or parent class is the existing class. The subclass-derived class or child class is the new class that inherits the properties and functions of an existing class.

In object-oriented programming, the inheritance relationship between classes is called a class hierarchy.

Inheritance is one of the key features of object-oriented programming. The principle of inheritance states that every subclass has shared properties with the parent class from which it was derived. The child class thus inherits all capabilities of the parent class and can add additional capabilities of its own.

Advantages of inheritance in C++

Some important advantages of inheritance are as follows:

Reusability

Inheritance allows the user to reuse existing code in many situations. A class can be created once and it can be reused again and again to create many sub-classes.

Saves time and effort

Inheritance is beneficial because it allows you to avoid writing the same classes over again, thereby saving you time and effort. Additionally, because existing classes can be used, it allows programs to run more smoothly and efficiently.

Increase program structure and reliability

If we do not use already existing classes, every time we create a new application we will have to spend time on compiling.

But now we have a superclass, and with this class, it is not necessary to compile our project every time when we need to create an application. As a result, our application will be more reliable and will be able to run faster.

Categories of Inheritance in C++

There are two categories of inheritance

Single Inheritance

A type of inheritance in which a child class is derived from a single-parent class is known as single inheritance. The child class in this inherits all data members and member functions of the parent class. It can also add further capabilities of its own.

image showing the single inheritance

Multiple Inheritance

A type of inheritance in which a child class is derived from multiple parent’s classes is known as multiple inheritances. The child class in this inheritance inherits all data members and member functions of all parent classes. It can also add further capabilities of its own.

image showing the multiple inheritance

Related FAQs

What is inheritance?

Inheritance is a key concept in OOPs that allows one class to obtain the properties of another. By utilizing inheritance, we are able to reuse the fields and methods already established in a parent or superclass.

How multiple inheritance is possible in C++?

 The multiple inheritance feature of C++ allows a class to inherit from more than one other class. The constructors of the inherited classes are called in the order in which they are inherited.

What is the purpose of inheritance?

The main purpose of inheritance is to reuse an existing class to create a new one. The new class inherits all the properties and methods of the original class.