Inheritance in C++

Video Tutorial
FREE
Inheritance  thumbnail
This video belongs to
Object Oriented Programming in Java Course Online
4 modules
Certificate

In C++, inheritance enables an object to automatically acquire all properties and behaviors from its parent object, allowing for the reuse, extension, or modification of attributes defined in another class. The class inheriting these members is the derived class, while the class providing the inherited members is the base class. This capability, a key aspect of Object-Oriented Programming, involves creating new classes ("derived" or "child" classes) from existing classes ("base" or "parent" classes), fostering code reusability and flexibility.

Why and When to use Inheritance in C++?

We use inheritance in C++ for the reusability of code from the existing class. C++ strongly supports the concept of reusability. Reusability is yet another essential feature of OOP(Object Oriented Programming).

It is always good to reuse something that already exists rather than trying to create the one that is already present, as it saves time and increases reliability.

We use inheritance in C++ when both the classes in the program have the same logical domain and when we want the class to use the properties of its superclass along with its properties.

For example, there is a base class or parent class named “Animal,” and there is a child class named “Dog,” so, here dog is an animal, so in “Dog class,” all the common properties of the “Animal” class should be there, along with its property of dog animal.

Modes of inheritance in C++

There are three modes of inheritance:

  • Public mode
  • Protected mode
  • Private mode

Public mode:

In the public mode of inheritance, when a child class is derived from the base or parent class, then the public member of the base class or parent class will become public in the child class also, in the same way, the protected member of the base class becomes protected in the child class, and private members of the base class are not accessible in the derived class.

Protected mode:

In protected mode, when a child class is derived from a base class or parent class, then both public and protected members of the base class will become protected in the derived class, and private members of the base class are again not accessible in the derived class. In contrast, protected members can be easily accessed in the derived class.

Private mode:

In private mode, when a child class is derived from a base class, then both public and protected members of the base class will become private in the derived class, and private members of the base class are again not accessible in the derived class.

Given table will summarize the above three modes of inheritance and show the Base class member access specifier when derived in all three modes of inheritance in C++:

Base class member access specifierType of Inheritence
PublicProtectedPrivate
PublicPublicProtectedPrivate
ProtectedProtectedProtectedPrivate
PrivateNot accessible(Hidden)Not accessible(Hidden)Not accessible(Hidden)

Types of inheritance in C++

Types of inheritance in CPP

There are five types of inheritance in C++:

Single Inheritance in C++:

When the derived class inherits only one base class, it is known as Single Inheritance.

Single Inheritance in C++ In the above diagram, A is a Base class, and B is a derived class. Here the child class inherits only one parent class.

Example of Single Inheritance:

Output

In the above example, Base is the class name and the parent class, which contains the property named salary and the value 900.

In the same way, there is another class named Derived, which is the child class, which inherits the property of the parent class and has its property named as a bonus which contains the value of 100.

In the child class, there is a function named sum(), which is used to add the salary and bonus. In the main function, an object is created named “x” of the “Derived” class which is a child class, and using that object, the properties, and the sum function are called from the derived class, which will add the salary and bonus and gives it as output.

Multiple Inheritance in C++

When a derived class(child class) inherits more than one base class(parent class), it is called multiple inheritance.

Multiple Inheritance in C++

In the above diagram, “A” and “B” are base classes, and “C” is a derived class that inherits more than one base class(parent class).

Example of Multiple Inheritance:

Output

The above program has two base classes named Base1 and Base2, which contain the properties salary and bonus.

A derived class named ”derived” inherits parent classes Base1 and Base2. In the derived class, the function name sum is used to give the sum of salary and bonus.

In the main function, there is an object named x that is created from a derived class called the sum() function, which will add bonus and salary and give it as output.

Diamond problem in Multiple Inheritance:

Diamond problem in Multiple Inheritance

In multiple inheritance, we can face a diamond problem. Let’s take an example in the above figure: “B” and “C” classes are inherited from a single Base class that is “A,” and then“D” is inherited from both “B'' and “C”, as this situation is creating a diamond shape, therefore, this problem is known as “diamond problem”.

Now here, “B” and “C” classes will have the member variable of class “A” as they are inherited from “A”, as “D” is inherited from “B” and “C” so it will contain two copies of “A’s” member variable that is one from “B” and one from “C”, thus causing ambiguities.

The compiler will get confused about which will be taken in D from the two copies of A’s member variable.

Solution of Diamond Problem:

The solution to the diamond problem is Virtual inheritance. It is a technique that ensures that only one copy of the superclasses or base class member variables is inherited by the second-level derivatives that are grandchild.

So in the above example, we can declare class “A” as a virtual base class. Now only one copy of the class “A” member variable will be copied to class “B” and “C”.

Multilevel Inheritance in C++:

When a derived(child) class inherits the base class and acts as the base class(parent class) to the other class, it is called Multilevel Inheritance. There can be any number of levels i.e any number of derived classes in multilevel inheritance.

Multilevel Inheritance in C++

In the above diagram, class ”B” is derived from class ”A,” and class ”B” is now the base class (parent class) for class “C,” and class “C” is derived from class “B”. Now class “C” inherits the property of Classes “A” and “B”.

Example of Multilevel Inheritance:

Output:

The above program contains a base class named BaseClass which contains the message. This is an example of Multilevel Inheritance, there is a derived class named DerivedClass, which inherits the base class(parent class), and then there is one more derived class named DerivedClass2, which inherits the last derived class.

In the main function, the object is created named obj of DerivedClass2. obj calls the print function of the base class, which is the topmost parent class. This shows that DerivedClass2 inherits the property of DerivedClass and BaseClass.

Hierarchical Inheritance in C++:

When more than one class is inherited from a single base class, it is called Hierarchical Inheritance.

Hierarchical Inheritance in C++

In the above diagram, “A” is a base class, and “B” and “C” are derived classes, which inherit the parent class “A”. “D” and “E” is further derived classes that inherited the base class “B,” which is derived class to class “A”, similarly “E” and “G” are the derived class that inherits base class “C,” which is derived class to “A”.

Example of Hierarchical Inheritance:

Output

In the above example, there is a base class named “Single_base_class,” which contains a data function that collects the input from the user. There is a derived class named “Derived1,” which extends the base class “Single_base_class”, Derived1 class contains a product function that is used to do a product of two numbers, here “x” and “y”.

There is one more derived class named “Derived2,” which also inherits the base class “Single_base_class” it contains the sum function to add the numbers, and in the main function object is created, which is used to call the function created in the derived classes and then we get the output as shown above.

Hybrid Inheritance in C++:

It is a combination of one or more types of inheritance.

image alt

The above diagram shows more than one type of inheritance or a combination of different types of inheritance.

Example of Hybrid Inheritance:

Output

In the above program, there is a base class named World, and then there is a derived class named Continent which extends the base class “World” here; we can see single inheritance.

There is a base class named Country, and there is a derived class named India which inherits the “Continent” and “Country” classes. Here we can see multiple inheritance. Hence this is an example of Hybrid inheritance.

Advantages of Inheritance in C++

The advantages of inheritance are:

  • Inheritance in C++ promotes Code reusability. When a derived class inherits the base class, then the derived class can access all the functionality, and the base class's code can be reused in the derived class.
  • It improves code readability as you don’t have to rewrite the same code repeatedly; hence, the code looks cleaner and readable.
  • It saves time and effort as the code is already written and is inherited; therefore, it saves time to write code again.
  • Inheritance supports extensibility as new classes can be easily added to existing classes.

Conclusion

Inheritance is a fundamental concept in the programming world, as we have seen how it is helpful as it increases the reusability and reliability of the code. We discussed the different modes and levels of inheritance and how they are used accordingly.

I hope you like this blog. If you find this blog insightful, don't forget to share it with your friends.