Inheritance in C++ - Scaler Topics (2024)

Overview

The capability of a class to inherit the properties of some other class is known as Inheritance. The class whose properties are inherited is called the base class, while the class which inherits the properties is known as the derived class.

Introduction

Suppose there is a class named Car; under the Car class, there are subclasses like BMW, Mercedes, etc.; under the Car name like Mercedes, there are further subclasses named as models of the cars.

This is a classic example of understanding Inheritance.

Now let’s understand what Inheritance is.

The capability of a class to inherit properties from another class is called Inheritance. Like in the example above, the Mercedes class inherits the property of the class Car.

Inheritance is one of the important fundamentals of Object Oriented Programming principles and is very useful. It helps in the reusability of code, which saves lots of time, making it an important concept to learn in the programming world.

A class is a blueprint that defines the variable and the methods common to all objects of a certain kind. Like in the example mentioned above Car is a class.

There are two types of classes in inheritance:

  • Parent Class or Base Class :- It is a class whose property is inherited by a subclass. Like in the example discussed above, Car is a parent class.

  • Child Class or Derived Class :- It is a class that inherits the property of another class. Like in the example discussed above, BMW and Mercedes are child class.

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++

Inheritance in C++ - Scaler Topics (1)

There are five types of inheritance in C++:

  • Single Inheritance
  • Multiple Inheritance
  • Multilevel Inheritance
  • Hybrid Inheritance
  • Hierarchical Inheritance

Single Inheritance in C++:

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

Inheritance in C++ - Scaler Topics (2)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.

Inheritance in C++ - Scaler Topics (3)

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:

Inheritance in C++ - Scaler Topics (4)

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.

Inheritance in C++ - Scaler Topics (5)

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.

Inheritance in C++ - Scaler Topics (6)

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.

Inheritance in C++ - Scaler Topics (7)

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.

As an expert in object-oriented programming (OOP) and C++, I can confidently provide a detailed analysis of the concepts presented in the article, showcasing a depth of knowledge and practical understanding.

Concepts Discussed:

1. Inheritance:

  • Definition: Inheritance is the capability of a class to inherit properties from another class. The class whose properties are inherited is the base class, and the inheriting class is the derived class.
  • Importance: Essential fundamental in OOP, promoting code reusability and saving development time.

2. Types of Classes in Inheritance:

  • Parent Class (Base Class): The class whose properties are inherited.
  • Child Class (Derived Class): The class that inherits properties from another class.

3. Why and When to Use Inheritance in C++:

  • Purpose: Reusability of code, a fundamental feature in OOP.
  • Advantages:
    • Code reusability.
    • Time and effort savings.
    • Improved code readability.
    • Supports extensibility.

4. Modes of Inheritance in C++:

  • Public Mode: Public and protected members of the base class become public and protected in the derived class, respectively.
  • Protected Mode: Both public and protected members of the base class become protected in the derived class.
  • Private Mode: Both public and protected members of the base class become private in the derived class.

5. Types of Inheritance in C++:

  • Single Inheritance: A derived class inherits from only one base class.
  • Multiple Inheritance: A derived class inherits from more than one base class.
  • Multilevel Inheritance: A derived class acts as a base class for another class, forming a chain.
  • Hierarchical Inheritance: More than one class is derived from a single base class.
  • Hybrid Inheritance: A combination of one or more types of inheritance.

6. Diamond Problem in Multiple Inheritance:

  • Issue: Ambiguities arise when a class inherits from two classes that share a common base class.
  • Solution: Virtual inheritance ensures only one copy of the base class is inherited.

7. Examples of Inheritance in C++:

  • Single Inheritance Example: Base class "Base," derived class "Derived" with an added property.
  • Multiple Inheritance Example: Classes "Base1" and "Base2," derived class "Derived" with added properties.
  • Multilevel Inheritance Example: Base class "BaseClass," derived classes "DerivedClass" and "DerivedClass2" forming a chain.
  • Hierarchical Inheritance Example: Base class "Single_base_class," derived classes "Derived1" and "Derived2."
  • Hybrid Inheritance Example: Classes "World," "Continent," and "India" with a combination of inheritance types.

8. Advantages of Inheritance in C++:

  • Code reusability.
  • Improved code readability.
  • Time and effort savings.
  • Supports extensibility.

9. Conclusion:

  • Summary: Inheritance is fundamental, enhancing code reusability and reliability. Different modes and types of inheritance cater to various programming needs.

As demonstrated, the article comprehensively covers the concept of inheritance in C++, exploring its types, modes, and practical applications. If you have any specific questions or need further clarification on any aspect, feel free to ask.

Inheritance in C++ - Scaler Topics (2024)
Top Articles
Latest Posts
Article information

Author: Frankie Dare

Last Updated:

Views: 5682

Rating: 4.2 / 5 (73 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Frankie Dare

Birthday: 2000-01-27

Address: Suite 313 45115 Caridad Freeway, Port Barabaraville, MS 66713

Phone: +3769542039359

Job: Sales Manager

Hobby: Baton twirling, Stand-up comedy, Leather crafting, Rugby, tabletop games, Jigsaw puzzles, Air sports

Introduction: My name is Frankie Dare, I am a funny, beautiful, proud, fair, pleasant, cheerful, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.