C++ Multiple, Multilevel and Hierarchical Inheritance


Introduction

Inheritance in C++ is a mechanism that allows one class to inherit the properties and behaviors (attributes and methods) of another class. In C++, there are various types of inheritance, including multiple inheritance, multilevel inheritance, and hierarchical inheritance. These inheritance types define how a derived class can inherit from one or more base classes and how the relationship between classes is structured.

Understanding the differences between multiple, multilevel, and hierarchical inheritance is essential for building robust object-oriented applications in C++. Let's explore these types of inheritance in detail.


1. Multiple Inheritance

Multiple inheritance occurs when a derived class inherits from more than one base class. In other words, a class can have multiple parent classes, and it inherits the members (attributes and methods) from all the base classes.

This type of inheritance can be useful in situations where a class needs to combine the functionality of two or more classes.

Syntax for Multiple Inheritance

class DerivedClass : public BaseClass1, public BaseClass2 {
    // Derived class members
};

Example of Multiple Inheritance:

#include <iostream>
using namespace std;

// Base class 1
class Animal {
public:
    void eat() {
        cout << "Animal eats!" << endl;
    }
};

// Base class 2
class Vehicle {
public:
    void drive() {
        cout << "Vehicle drives!" << endl;
    }
};

// Derived class inheriting from both Animal and Vehicle
class Car : public Animal, public Vehicle {
public:
    void honk() {
        cout << "Car honks!" << endl;
    }
};

int main() {
    Car car;
    car.eat();     // Inherited from Animal class
    car.drive();   // Inherited from Vehicle class
    car.honk();    // Defined in Car class

    return 0;
}

Explanation:

  • The Car class inherits from both Animal and Vehicle.
  • The derived class has access to the methods eat() from Animal and drive() from Vehicle.
  • The Car class also has its own method honk().

Advantages of Multiple Inheritance:

  • It allows a derived class to inherit functionality from multiple base classes.
  • It helps in modeling complex real-world entities that have characteristics of multiple categories.

Issues with Multiple Inheritance:

  • Diamond Problem: This occurs when a derived class inherits from two classes that both inherit from a common base class. It can lead to ambiguity and duplication of data.

We can solve this using virtual inheritance, but we will cover that in a later section.


2. Multilevel Inheritance

Multilevel inheritance occurs when a class is derived from another derived class, creating a chain of inheritance. In other words, a class can inherit from a derived class, which in turn inherits from another base class.

Syntax for Multilevel Inheritance

class DerivedClass1 : public BaseClass {
    // Derived class members
};

class DerivedClass2 : public DerivedClass1 {
    // Derived class members
};

Example of Multilevel Inheritance:

#include <iostream>
using namespace std;

// Base class
class Animal {
public:
    void eat() {
        cout << "Animal eats!" << endl;
    }
};

// Derived class from Animal
class Mammal : public Animal {
public:
    void giveBirth() {
        cout << "Mammal gives birth!" << endl;
    }
};

// Further derived class from Mammal
class Dog : public Mammal {
public:
    void bark() {
        cout << "Dog barks!" << endl;
    }
};

int main() {
    Dog dog;
    dog.eat();        // Inherited from Animal
    dog.giveBirth();  // Inherited from Mammal
    dog.bark();       // Defined in Dog

    return 0;
}

Explanation:

  • The Dog class is derived from Mammal, and Mammal is derived from Animal.
  • The Dog class can access methods from both the Mammal and Animal classes.
  • This demonstrates how inheritance can create a hierarchy of classes.

Advantages of Multilevel Inheritance:

  • It allows the creation of a more specialized class by extending the functionality of an already existing class.
  • It forms a clear hierarchy and is useful in cases where the child classes share a common ancestor.

3. Hierarchical Inheritance

Hierarchical inheritance occurs when multiple classes are derived from a single base class. In this case, one base class serves as a parent for multiple derived classes.

Syntax for Hierarchical Inheritance

class DerivedClass1 : public BaseClass {
    // Derived class members
};

class DerivedClass2 : public BaseClass {
    // Derived class members
};

Example of Hierarchical Inheritance:

#include <iostream>
using namespace std;

// Base class
class Animal {
public:
    void eat() {
        cout << "Animal eats!" << endl;
    }
};

// Derived class 1 from Animal
class Dog : public Animal {
public:
    void bark() {
        cout << "Dog barks!" << endl;
    }
};

// Derived class 2 from Animal
class Cat : public Animal {
public:
    void meow() {
        cout << "Cat meows!" << endl;
    }
};

int main() {
    Dog dog;
    dog.eat();  // Inherited from Animal class
    dog.bark(); // Defined in Dog class

    Cat cat;
    cat.eat();  // Inherited from Animal class
    cat.meow(); // Defined in Cat class

    return 0;
}

Explanation:

  • Both the Dog and Cat classes are derived from the Animal base class.
  • Both derived classes inherit the eat() method from the Animal class.
  • The Dog class defines the method bark(), and the Cat class defines meow().

Advantages of Hierarchical Inheritance:

  • It allows code reusability from the common base class while extending the functionality of multiple derived classes.
  • It simplifies the design when multiple classes share common behaviors (from the base class).

4. When to Use Multiple, Multilevel, and Hierarchical Inheritance

  • Multiple Inheritance: Use this when a class needs to inherit characteristics from multiple base classes. However, it can lead to complications (such as the diamond problem), and you should handle it carefully.

  • Multilevel Inheritance: Use when you need to create a hierarchy where one class is derived from another, and further derived classes continue the chain. This type of inheritance is often used to represent a clear hierarchical relationship.

  • Hierarchical Inheritance: Use when multiple derived classes share a common base class. This is appropriate when you have different subclasses with similar behaviors that can inherit common functionality from a single parent class.