Types of Inheritance in C++

Inheritance is one of the four fundamental principles of Object-Oriented Programming (OOP) in C++. It allows a class (child class or derived class) to acquire properties and behaviors from another class (parent class or base class). This promotes code reusability, modularity, and maintainability.

C++ supports various types of inheritance, each with its own use case and application. In this article, we will explore the different types of inheritance in C++ with explanations and examples.

1. Single Inheritance

In single inheritance, a derived class inherits from only one base class. This is the simplest form of inheritance and is useful when a derived class needs to extend the functionality of a single base class.

Example:

#include <iostream>
using namespace std;

class Base {
public:
    void show() {
        cout << "This is the base class" << endl;
    }
};

class Derived : public Base {
public:
    void display() {
        cout << "This is the derived class" << endl;
    }
};

int main() {
    Derived obj;
    obj.show();    // Inherited function
    obj.display(); // Own function
    return 0;
}

Output:

This is the base class
This is the derived class

2. Multiple Inheritance

In multiple inheritance, a derived class inherits from more than one base class. This enables the derived class to combine the functionalities of multiple base classes. However, it may lead to ambiguity if base classes have methods with the same name.

Example:

#include <iostream>
using namespace std;

class Base1 {
public:
    void show() {
        cout << "This is Base1" << endl;
    }
};

class Base2 {
public:
    void display() {
        cout << "This is Base2" << endl;
    }
};

class Derived : public Base1, public Base2 {
};

int main() {
    Derived obj;
    obj.show();    // From Base1
    obj.display(); // From Base2
    return 0;
}

Output:

This is Base1
This is Base2

Potential Issue: If Base1 and Base2 have a function with the same name, the derived class must use scope resolution (Base1::function();) to resolve ambiguity.

3. Hierarchical Inheritance

In hierarchical inheritance, multiple derived classes inherit from a single base class. This is useful when multiple classes need to share common features from a single base class.

Example:

#include <iostream>
using namespace std;

class Base {
public:
    void show() {
        cout << "This is the base class" << endl;
    }
};

class Derived1 : public Base {
};

class Derived2 : public Base {
};

int main() {
    Derived1 obj1;
    Derived2 obj2;
    obj1.show(); // Inherited from Base
    obj2.show(); // Inherited from Base
    return 0;
}

Output:

This is the base class
This is the base class

4. Multilevel Inheritance

In multilevel inheritance, a derived class inherits from another derived class, forming a chain of inheritance.

Example:

#include <iostream>
using namespace std;

class Base {
public:
    void show() {
        cout << "This is the base class" << endl;
    }
};

class Intermediate : public Base {
public:
    void display() {
        cout << "This is the intermediate class" << endl;
    }
};

class Derived : public Intermediate {
};

int main() {
    Derived obj;
    obj.show();    // From Base
    obj.display(); // From Intermediate
    return 0;
}

Output:

This is the base class
This is the intermediate class

5. Hybrid (Virtual) Inheritance

In hybrid inheritance, multiple types of inheritance are combined. It often leads to the diamond problem, where a derived class inherits the same base class multiple times via different paths.

To avoid redundancy, virtual inheritance is used, ensuring that the base class appears only once in the inheritance hierarchy.

Example:

#include <iostream>
using namespace std;

class Base {
public:
    void show() {
        cout << "This is the base class" << endl;
    }
};

class Derived1 : virtual public Base {
};

class Derived2 : virtual public Base {
};

class FinalDerived : public Derived1, public Derived2 {
};

int main() {
    FinalDerived obj;
    obj.show(); // No ambiguity due to virtual inheritance
    return 0;
}

Output:

This is the base class

Conclusion

Inheritance in C++ plays a crucial role in OOP by promoting code reuse and modularity. The different types of inheritance—single, multiple, hierarchical, multilevel, and hybrid—offer various advantages and challenges.

  • Single Inheritance is simple and easy to use.
  • Multiple Inheritance allows a class to inherit from multiple base classes but may cause ambiguity.
  • Hierarchical Inheritance enables multiple derived classes to share common functionality.
  • Multilevel Inheritance forms an inheritance chain.
  • Hybrid Inheritance combines multiple inheritance types and often requires virtual inheritance to avoid redundancy.

Understanding these inheritance types will help developers design efficient and maintainable C++ programs.

Related Articles:

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *