• Articles
  • Tutorials
  • Interview Questions

Hierarchical Inheritance in C++: Syntax & Implementation

Hierarchical Inheritance in C++: Syntax & Implementation

Are you tired of writing the same code again and again? Hierarchical inheritance in C++ offers a solution! In this blog, we will cover all the major and basic features of hierarchical inheritance in C++. Read the blog ahead and resolve your doubts.

Table of Contents

Watch the video below to understand C programming in detail:

Video Thumbnail

What is Hierarchical Inheritance in C++?

Hierarchical inheritance in object-oriented programming refers to the structure where multiple classes are derived from a single superclass or base class. It forms a hierarchy of a family tree, where each derived class inherits attributes and behaviours from the same parent class but may introduce its own unique features. This arrangement allows for shared functionalities among related classes while permitting individual specialization within each subclass and promoting code reusability and organization, enhancing the flexibility and maintainability of software. 

Hierarchical Inheritance

Do you want to jumpstart your career in computer programming? Enroll in our C Programming Course and gain the skills to succeed!

Visibility Modes in Hierarchical Inheritance in C++

In C++, the visibility modes in hierarchical inheritance refer to how the derived class accesses the members of its base class. There are three visibility modes:

Public: With public inheritance, public members of the base class remain public in the derived class. Private members stay inaccessible, and protected members remain protected.

Syntax

class parent_class

{

    //data members

    //member functions

};

class child_class1 : public parent_class

{

    //data members

    //member functions

};

class child_class2 : public parent_class

{

    //data members

    //member functions

};

Protected: In this mode, both the public and protected members of the base class become protected in the derived class. Private members remain inaccessible.

Syntax

class parent_class

{

    //data members

    //member functions

};

class child_class1 : protected parent_class

{

    //data members

    //member functions

};

class child_class2 : protected parent_class

{

    //data members

    //member functions

};

Private: All members of the base class become private in the derived class, regardless of their original visibility. This mode hides all base class members from outside access through the derived class. 

Syntax

class parent_class

{

    //data members

    //member functions

};

class child_class1 : private parent_class

{

    //data members

    //member functions

};

class child_class2 : private parent_class

{

    //data members

    //member functions

};

Each visibility mode offers different levels of accessibility and encapsulation between the base and derived classes.

Check out the Top 20 C++ Project Ideas

Syntax of Hierarchical Inheritance in C++

In C++, hierarchical inheritance involves creating a class that inherits from more than one class. Here’s a basic syntax example:

class Base {

    // Base class members and methods

};

class Derived1 : public Base {

    // Derived1 class inherits publicly from Base

    // Additional members and methods specific to Derived1

};

class Derived2 : public Base {

    // Derived2 class also inherits publicly from Base

    // Additional members and methods specific to Derived2

};

// Usage

int main() {

    Derived1 obj1;

    Derived2 obj2;

    // Objects of Derived1 and Derived2 have access to Base class members as well as their own.

    return 0;

}

In this example, both ‘Derived1’ and ‘Derived2’ inherit publicly from the ‘Base’ class, meaning they have access to Base’s public members. This creates a hierarchical relationship where both ‘Derived1’ and ‘Derived2’ inherit from the same ‘Base’ class.

Check out the top C++ Interview Questions to ace your next interview!

Practical Implementation of Hierarchical Inheritance

Refer to the code below for the practical implementation of hierarchical inheritance in C++:

#include <iostream> 

using namespace std; 

// Base class 

class Animal { 

public: 

    Animal() 

    { 

        cout << "This is an Animal" << endl; 

    } 

}; 

// subclass derived from Base Class(Vehicle) 

class Lion: public Animal { 

public: 

    Lion() 

    { 

        cout << "This is a Lion" << endl; 

    } 

}; 

// sub class derived from Base Class(Vehicle) 

class Tiger: public Animal { 

public: 

    Tiger() 

    { 

        cout << "This is a Tiger" << endl; 

    } 

}; 

// sub class derived from Base Class(Car) 

class Pig: public Lion { 

public: 

    Pig() 

    { 

        cout << "This is a Tiger Pig" << endl; 

    } 

}; 

// main function 

int main() 

{ 

    // creating object of subclass will 

    // invoke the constructor of base class 

   Pig obj1; 

    return 0; 

}

Output:

This is an Animal

This is a Lion

This is a Tiger Pig

Understand what is meant by state management in ASP.NET by knowing it’s applications, types, examples, and concepts in detail.

Advantages & Disadvantages of Hierarchical Inheritance

Every technology has some advantages and disadvantages; similarly, hierarchical inheritance has its share of pros and cons. Some of them are given below: 

Advantages

Code Reusability: Allow the reuse of code among multiple classes as subclasses inherit behavior and attributes from a single patent class.

Ease of Maintaninence: Changes made to the parent class automatically propagate to its subclasses, reducing the need for repetitive updates.

Organized Structure: Offers a clear and organized hierarchy, making it easier to understand the relationship between different classes.

Facilitates Polymorphism: Hierarchical inheritance allows for polymorphic behavior, meaning objects of different classes in the same inheritance hierarchy can be treated uniformly through their common parent class, enabling more flexible and efficient code.

Disadvantages

Rigidity: This can lead to a rigid structure where changes in the parent class might impact all subclasses, potentially causing unwanted issues.

Complexity: As the hierarchy grows deeper, it can become more challenging to manage and understand the relationship between classes.

Inheritance Overuse: Overdependency on inheritance might lead to a more complex and tightly coupled design, impacting flexibility and maintainability.

Wrap-Up

Hierarchical inheritance offers a powerful organizational structure in object-oriented programming, facilitating the creation of class hierarchies where subclasses inherit common characteristics from a shared superclass. This approach promotes code reusability, modularity, and ease of maintenance by allowing for both shared functionalities among related classes and individual customization within each subclass. It’s a fundamental concept that enhances the flexibility and scalability of software systems, contributing to more efficient and manageable codebases.

FAQs

Can a derived class have more than one base class in hierarchical inheritance?

No, in a strict hierarchical inheritance, a derived class can have only one immediate base class. However, multiple classes can be inherited from the same base class, creating a hierarchical structure.

How does a derived class access the members of the base class in hierarchical inheritance?

The derived class can access the public and protected members of its base class using the dot (.) operator or through member functions, maintaining the inheritance chain.

Is it possible to have multiple levels of inheritance in a hierarchical structure?

Yes, hierarchical inheritance can have multiple levels where subclasses further derive into other subclasses, forming a hierarchical tree structure.

What are the main challenges or issues with hierarchical inheritance in C++?

One main challenge is the potential for creating overly complex class hierarchies, leading to tight coupling and maintenance difficulties. Additionally, changes in the base class might affect multiple derived classes.

How can hierarchical inheritance enhance code reusability?

Hierarchical inheritance allows derived classes to inherit attributes and behaviors from a common base class, promoting code reuse and reducing redundancy in the codebase.

Can a derived class override the methods of the base class in hierarchical inheritance?

Yes, a derived class can override the virtual methods of the base class, providing its implementation and customizing the behavior while maintaining the hierarchical structure.

Course Schedule

Name Date Details
Python Course 23 Nov 2024(Sat-Sun) Weekend Batch View Details
30 Nov 2024(Sat-Sun) Weekend Batch
07 Dec 2024(Sat-Sun) Weekend Batch

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration.