Hierarchical Inheritance in C++ with Real-life Analogy

Hierarchical Inheritance in C++ with Real-life Analogy

7 mins read3.5K Views Comment
clickHere
Updated on Jun 20, 2023 18:13 IST

If you wanna know about Hierarchical inheritance.Then you have landed on the right page.This article will explain this article with real life analogy and programming example with explanation.

2023_02_MicrosoftTeams-image.jpg

Inheritance is one of the most important concepts in computer science. It enables a class to extend the functionality of another class. It’s one of the most powerful ways to reuse code and make it accessible to multiple programs.

Inheritance is one of the most important concepts in computer science. It enables a class to extend the functionality of another class. It’s one of the most powerful ways to reuse code and make it accessible to multiple programs. In this paper, I’ll discuss inheritance in C++ and how to build robust software.

The most common form of inheritance in C++ is called hierarchical inheritance. This form of inheritance enables a class to inherit from another class at a higher hierarchy level. It’s named after the tree structure it resembles; each node in this structure has its level of inheritance.

Table of contents

What is Hierarchical Inheritance?

Hierarchical Inheritance is a type of inheritance in Object Oriented Programming (OOP) in which a derived class (subclass) is created from more than one base class (superclass). This type of inheritance allows the derived class to inherit all of the base classes’ variables, methods and properties, thus creating a hierarchy of classes. An example of hierarchical inheritance is when a ‘Car’ class is derived from both a ‘Vehicle’ class and a ‘Gasoline’ class. The ‘Car’ class would then be able to inherit both the properties of the ‘Vehicle’ class (such as its color and speed) and the properties of the ‘Gasoline’ class (such as its fuel efficiency). The main advantage of hierarchical inheritance is that it helps create more organized, efficient, and reusable code.

Syntax of Hierarchical Inheritance

 
class base_class
{
//data members
//member functions
};
class derived_class1 : visibility_mode base_class
{
//data members
//member functions
};
class derived_class2 : visibility_mode base_class
{
//data members
//member functions
};
Copy code

Real-life analogy of Hierarchical Inheritance

2023_01_MicrosoftTeams-image-29.jpg

Think about the company setting. The hierarchical structure can be seen in how departments, teams, and individuals are arranged in order of an authority. The CEO is at the top of the hierarchy, followed by Vice Presidents, Directors, Managers, and then individual employees. 

In C++, this is represented by a class hierarchy. You have classes at the top of the hierarchy (like CEO), and then you have classes nested within them (like your managers). This nested relationship allows you to share common functionality between classes while still maintaining their individual identities.

Also check: C++ Online Courses & Certifications

Must check: Free C++ Courses Online

When to use Hierarchical Inheritance in C++

In this tutorial, you’ve got a good overview of how hierarchical inheritance works in C++. Now the question is, when should you use it?

  • If you have multiple classes that share the same attributes and methods, then hierarchical inheritance is the way to go. It allows you to create one parent class that stores all of these shared elements(data members or member functions) and then several child classes that inherit from the parent. This makes it much easier to track all your data and saves time.
  • However, there are better solutions than hierarchical inheritance if the classes have different attributes and methods. Instead, you can use multiple inheritance or aggregation.

At this point, you better understand hierarchical inheritance in C++ and when to use it. In the next section, we’ll look at how to implement it in your own code!

Advantages of Hierarchical Inheritance

1. It allows for code reuse and refactoring, as the same code can be reused in multiple classes.

2. It makes it easier to extend the code, as the same code can be used to create different classes that all have access to the same members.

3. It helps create a more organized and efficient code, as all the members of the base classes are available to the derived classes.

4. It allows a single member to be used in multiple derived classes without having to rewrite the code multiple times.

5. It creates a clear hierarchy of classes, making debugging and maintaining the code easier.

Disadvantages of Hierarchical Inheritance

1. It can lead to a lot of code duplication, as the derived classes will have to duplicate the code from the base classes to use the same methods.

2. It can create a rigid hierarchy of classes, making it difficult to extend the code in the future without having to rewrite lots of code.

3. It can be difficult to debug and maintain, as the same errors can occur in multiple classes.

4. It can lead to tight coupling between classes, making it easier to make changes to the code by breaking other parts of the code.

5. Changes to parent classes may have unexpected effects on unrelated child classes, leading to increased complexity and debugging time.

Visibility Modes in Hierarchical Inheritance in C++

The visibility modes in hierarchical inheritance in C++ are Public, Protected, and Private. This means that the base class members (which includes functions and data variables) can be inherited as public, protected, or private according to the user’s needs.

Public: Base class members declared public will be inherited as public members in derived classes and can be accessed by both the derived class and any external user.

Protected: Base class members declared protected will only be accessible within the derived classes(of that base class) unless it is explicitly declared public within the derived classes.

Private: Base class members who have been declared private will only remain accessible within the base class itself, not to any of its derived classes or an external user.

Hierarchical Inheritance Program in C++

 
#include <iostream>
using namespace std;
// Base class
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
// subclass derived from Base Class(Vehicle)
class Car: public Vehicle {
public:
Car()
{
cout << "This is a Car" << endl;
}
};
// sub class derived from Base Class(Vehicle)
class Bus: public Vehicle {
public:
Bus()
{
cout << "This is a Bus" << endl;
}
};
// sub class derived from Base Class(Car)
class Maruti: public Car {
public:
Maruti()
{
cout << "This is a Maruti Car" << endl;
}
};
// main function
int main()
{
// creating object of subclass will
// invoke the constructor of base class
Maruti obj1;
return 0;
}
Copy code

Explanation

Hierarchical Inheritance is a type of inheritance in C++ where one base class is inherited by more than one derived class. In this type of inheritance, all the derived classes will inherit all the properties of the base class i.e Vehicle

In the above example, the base class is ‘Vehicle’ and the derived classes are ‘Car’, ‘Bus’, and ‘Maruti’. All the derived classes will inherit the properties and methods of the base class ‘Vehicle.’ This is done by using the keyword ‘public’ to inherit the base class members. The derived class ‘Maruti’ will also inherit the members of the other derived classes ‘Car’ and ‘Bus’.

Conclusion

In short, you are taking an important step to mastering C++ programming by understanding the basic concepts behind a hierarchical inheritance. With a strong foundation in place, you will be able to create more complex and sophisticated code, and solve more complex problems. Remember, practice makes perfect, so continue to apply these concepts in your programming projects, and you will soon be on your way to becoming a C++ expert.

FAQs

How can hierarchical inheritance be useful in software development?

Hierarchical inheritance allows for the creation of specialized classes that inherit and build upon the characteristics and behavior of a common base class. This can be beneficial in software development when modeling complex systems or domains, as it provides a structured and organized way to represent relationships and promote code reuse. It also facilitates the application of concepts like polymorphism and encapsulation, enhancing the overall flexibility and maintainability of the codebase.

Are there any limitations or considerations when using hierarchical inheritance?

One limitation of hierarchical inheritance is the potential for code duplication. If multiple derived classes need to override the same method from the base class, it may lead to redundant code. Additionally, a deep and complex hierarchy can make the codebase harder to maintain and understand. It's important to carefully design and structure the class hierarchy to ensure clarity and avoid excessive coupling between classes.

How does hierarchical inheritance work in C++?

In C++, hierarchical inheritance is achieved by using the public or protected access specifier while inheriting from the base class. The derived classes inherit the public and protected members of the base class, maintaining the hierarchical relationship. Each derived class can further extend the functionality of the base class or override its methods as needed.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio