Inheritance is one of the useful properties of the objective oriented language. Like other objective languages c++ also support the inheritance. Inheritance in c++ allows us to create a class in term of another class that means it creates the hierarchy of the class. It makes the easier to create and maintain the application.

One of the good features of the inheritance it allows the derived class to access features of the base class except for some exception which we will discuss later.




Inheritance helps us to reuse the code which makes the development easy and faster. In the other word, we can say we don’t’ need to create a new member function and member variable we just use the existing class member function and variable after the inherit of the existing class.

Note: Inheritance should be used when you able to reuse the code of the base class that means base class and derived class should have the “is a” relationship, not a “has-a” relationship.

What are a Base and derived class?

In inheritance existing class is called the base or parent class and newly created class is called derived or child class. A derived class can be inherited from more than one class, it all the thing depends on the requirements.When we have created a derived class then derived class able to reuse the code of the base class.



Syntax of inheritance

The syntax of inheritance in C++ is very simple. You just create a class as usual but before the opening of braces of the body of class just put a colon and name of the base class with the access specifier.

class derived-class : access-specifier  base-class

Here access specifier can be public, private or protected, derived class is newly created class and base class is already existing class.

Why is inheritance required?

Suppose in a program you have required to collect the information of cow, dog, and cat. This information is like their speed, price, and diet. So you have to create three class here to save the information of cow, dog and cat and each class contain the function to calculate to calculate their speed price and diet.

www.aticleworld.com

From the above image, it is clear that these three class use the same three function to calculate the speed, price, and diet. This type of approach is not good for development and it reduces the code reusability and increases the development time.

We can solve this problem with the help of inheritance. We just need to create a base class which will consist all the three-member function and just inherits this base class for the class which is used to store the information of the cow, dog, and cat.

www.aticleworld.com



Access control (Modes of inheritance in c++)

In c++ derive class only access the non-private member of the base class that means a private member of the base class is not accessible in the derived class.

At the time of inheritance if the access specifier is public then all non-private member of the base class will be public in derived class and if the access specifier is private or protected then inherited member of the base class will be private and protected in derived class.

In below table, I am summarizing all of the access specifier and inheritance types combinations:

www.aticleworld.com

In c++, a derived class can access all the non-private member of the base but there are some exceptions which I am mentioning below.

  • A derived class does not access the friend function of the base class.
  • A derived class does not access the constructor, copy constructor, destructor of the base class.
  • A derived class does not access the overloaded operator of the base class.

Types of inheritance in c++

Single inheritance in c++

This is a basic type of inheritance in which only one derived class inherit from one base class. In below example, class B is derived from class A.

www.aticleworld.com


Multiple inheritance in c++

In which one class is derived from more than two class. In below example class C is derived from class A and class B.

www.aticleworld.com






Hierarchal inheritance in c++

In which more than one class is derived from the same base class. In below example, class B and class C is derived from same base class A.

www.aticleworld.com


Multilevel inheritance in c++

In which derived class is derived from another derived class. In below example class C is derived from another derived class B.

www.aticleworld.com


Hybrid inheritance in c++

Hybrid inheritance is the composition of more than one inheritance. Below example is the composition of multilevel and hierarchal inheritance.

www.aticleworld.com






Advantage of inheritance in c++

I have already discussed at the beginning of the article, inheritance minimizes the development cycle of the product to avoid the duplicate code in the project. It also arranges the code in a better way which increases the readability of the code and provides the flexibility to the user to make any changes easily.

In below, I am describing some beneficial features of inheritance in c++.

  • Reusability: It provides the facility to the derived class to use the public method of the base class without the rewriting the methods.
  • Overriding: It is a very useful feature of an objective oriented programming. Without the help of inheritance, we cannot use this property.
  • Extensibility: It is another advantage of the inheritance.It extending the base class logic as per business logic of the derived class.
  • Data hiding: It is also a good feature of the inheritance which provides the facility to the base class to decide which data to keep private that child class would not be able to change.

Disadvantage of inheritance in c++

  • In Inheritance, both base class and child classes are tightly coupled to each other. Hence If you change anything in the code of parent class, it will get effects to the all the child classes.
  • In a class hierarchy, many data members remain unused and the memory allocated to them is not utilized. Hence affect the performance of your program if you have not implemented inheritance correctly.