Object-Oriented Programming (C++)— Inheritance | The Easy Way (Part III)

Rahul Sharma
6 min readMay 24, 2020

--

Base Class | Derived Class — Photo by Aditya Romansa on Unsplash

What’s up fellas? This what you see is the third part of my Object-Oriented Programming concepts series. I would be covering the concept of Inheritance today which is one of the pillars of OOP Concepts.
If you missed the first two parts, nothing to worry, I am leaving the links below:

Let’s get going!
Starting from the basics, what Inheritance means and why in the world do we want to inherit?
Well, if we talk in terms of Classes, Inheritance simply means deriving a new class from an old one. The old class is referred to as the Base Class and the new one is called Derived Class or Subclass.
We need inheritance because no one wants to redo/reimplement a function that has already been implemented! I mean who would want that?
Reusability is one of the most important features of Inheritance. It is not always about doing extra work but the already done work might have also been tested well for any bugs and you obviously don’t want to go through that painful cycle again! Hence, just Reuse.

Forms of Inheritance

A parent class can be inherited by a child class in one of the following forms:
1) Single Inheritance
2) Multiple Inheritance
3) Hierarchical Inheritance
4) Multilevel Inheritance
5) Hybrid Inheritance

The below diagram will help you understand these forms in a nutshell.

Forms of Inheritance

Diving deep inside inheritance, it becomes important for us to understand which components within a base class can be inherited and used by the child class because a base class can contain multiple types of components like private, protected, and public. Let’s try to work this out with the help of a simple program, inherited from my mind! :P

One important thing to note in the above code is that the Person class has been inherited privately. The default form of inheritance in private, hence we could have skipped writing private. Moreover, with this type of inheritance, all the members of class Person now become only privately accessible members (except name & age, because they are too private) of the class Employee. If you are now having questions like what would happen if it was inherited publicly or in a protected way? We’ll be discussing these very soon! Hold back!

Here, we have used both base and derived parameterized constructors. The base derived constructor is executed first.

Don’t be afraid of the virtual keyword. We will be discussing it soon! But before that, it is important to know that there are three types of access specifiers present in C++ to encapsulate the members of a class.

1) PrivateOptional | Visible to member functions within the class only.
2) ProtectedVisible to member functions of its own and derived class.
3) Public Visible to all functions in the program

Different classes can be inherited in one of the above ways, and all its members are inherited differently based on how the base class is inherited. The below diagram will clear all your doubts about types of inheritance and accessible members of a class when inherited.

Types of Inheritance

I hope we are on the same page till now! Moving on, if you hadn’t had asked yourself what would happen if two same name members are present in both the parent and the child class. Well, now is the time!
Let’s discuss the ambiguity resolution in inheritance. For an easy understanding of the concept, I have chosen a single inheritance example below:

  1. Ambiguity Resolution in Inheritance

Note that, we have used the scope resolution operator here (::) to call the parent class method.

Since we are already talking about Ambiguity resolution, let’s discuss a case where all three kinds of inheritance, i.e, multilevel, multiple, and hierarchical inheritances are involved.

Multipath Inheritance

Here, class ‘D’ has two direct base classes, ‘B’ and ‘C’ which themselves have a common base class ‘A’.
Class ‘D’ inherits the properties of ‘A’ is sometimes referred to as an indirect base class.
This scenario here has a problem. All the protected and public members of class ‘A’ are inherited twice into ‘D’ from both ‘B’ and ‘C’. This leads to ambiguity as there are duplicate sets of the members of class ‘A’ inherited within class ‘D’.

The solution to this particular problem is making the common base class (ancestor class) as a Virtual Base Class while declaring the direct or indirect base classes.

Virtual Base Class

When a class is made a virtual base class, C++ takes necessary care to see that only one copy of that class is inherited, regardless of how many inheritance paths exist between the virtual base class and a derived class.

Let us try to understand this with the help of code because that’s how we really get the feel! ;)

In the above code, we have to include only one copy of the class Student’s members within the class ReportCard with the help of the virtual base class concept. Feel free to explore this on your own!

Moving forward, there’s this one last concept that we’ll be touching, Abstract Classes.

Abstract classes are nothing but C++ interfaces that describe the behavior or capabilities of a class without committing to a particular implementation of that class. A class is made abstract by declaring at least one of its functions as a pure virtual function. A pure virtual function is specified by placing a “=0” in its declaration.

The purpose of an abstract class is to provide an appropriate base class from which other classes can inherit. Abstract classes cannot be used to instantiate objects and serve only as an interface. Attempting to instantiate an object of an abstract class causes a compilation error.
Let’s try to understand this with the help of an example:

Note that the getArea() method within the base class Shape is a pure virtual function and thus the class Shape is an abstract class.

This my friend, marks the end of our discussion regarding Inheritance. I tried to cover a lot about Inheritance within a single article so that we don’t waste our time searching for different resources.

I hope you liked it!

I would be posting a few more blogs that would cover Abstraction, Encapsulation, and Polymorphism. Don’t worry, I will update the links here once I am done! :)

Meanwhile, did you know you can make me smile by clapping for this post?

About me? I am a tech lover! I love to read, implement and explain :)

--

--

Rahul Sharma

An enthusiastic full stack web developer and motivator!