C++ Multilevel Inheritance

Welcome to The Coding College! In this tutorial, we’ll explore Multilevel Inheritance in C++. This concept extends the basic idea of inheritance, enabling a derived class to inherit from another derived class, creating a chain of inheritance.

What Is Multilevel Inheritance?

Multilevel Inheritance is a type of inheritance where a class is derived from a derived class. In this way, a chain of inheritance is created where:

  • The base class acts as the parent for an intermediate derived class.
  • The intermediate derived class becomes the parent for a further derived class.

Representation:

BaseClass  
    ↑  
IntermediateDerivedClass  
    ↑  
DerivedClass  

Syntax of Multilevel Inheritance

class BaseClass {
    // Base class members
};

class IntermediateDerivedClass : public BaseClass {
    // Intermediate derived class members
};

class DerivedClass : public IntermediateDerivedClass {
    // Final derived class members
};

Example: Multilevel Inheritance

Let’s consider a simple example of Animals:

#include <iostream>
using namespace std;

// Base Class
class Animal {
public:
    void eat() {
        cout << "This animal eats food." << endl;
    }
};

// Intermediate Derived Class
class Mammal : public Animal {
public:
    void giveBirth() {
        cout << "This mammal gives birth to young ones." << endl;
    }
};

// Final Derived Class
class Dog : public Mammal {
public:
    void bark() {
        cout << "The dog barks." << endl;
    }
};

int main() {
    Dog myDog;

    myDog.eat();      // From Animal class
    myDog.giveBirth(); // From Mammal class
    myDog.bark();      // From Dog class

    return 0;
}

Output:

This animal eats food.  
This mammal gives birth to young ones.  
The dog barks.  

Benefits of Multilevel Inheritance

  1. Code Reusability: Classes can reuse functionality from multiple levels of hierarchy.
  2. Organization: Helps organize related classes into a structured hierarchy.
  3. Extensibility: New functionality can be added to derived classes without modifying existing ones.

Example: Multilevel Inheritance in Vehicles

#include <iostream>
using namespace std;

// Base Class
class Vehicle {
public:
    void move() {
        cout << "This vehicle can move." << endl;
    }
};

// Intermediate Derived Class
class Car : public Vehicle {
public:
    void fuelType() {
        cout << "This car uses petrol or diesel." << endl;
    }
};

// Final Derived Class
class SportsCar : public Car {
public:
    void speed() {
        cout << "This sports car is very fast." << endl;
    }
};

int main() {
    SportsCar myCar;

    myCar.move();      // From Vehicle class
    myCar.fuelType();  // From Car class
    myCar.speed();     // From SportsCar class

    return 0;
}

Output:

This vehicle can move.  
This car uses petrol or diesel.  
This sports car is very fast.  

Protected Members in Multilevel Inheritance

Protected members in the base class can be accessed in all derived classes within the hierarchy.

Example:

#include <iostream>
using namespace std;

// Base Class
class Person {
protected:
    string name;

public:
    void setName(string n) {
        name = n;
    }
};

// Intermediate Derived Class
class Employee : public Person {
protected:
    int employeeID;

public:
    void setEmployeeID(int id) {
        employeeID = id;
    }
};

// Final Derived Class
class Manager : public Employee {
public:
    void displayDetails() {
        cout << "Manager Name: " << name << ", ID: " << employeeID << endl;
    }
};

int main() {
    Manager mgr;

    mgr.setName("Alice");
    mgr.setEmployeeID(101);
    mgr.displayDetails();

    return 0;
}

Output:

Manager Name: Alice, ID: 101  

Key Points to Remember

  1. Multilevel inheritance allows creating a hierarchy of classes.
  2. A derived class can inherit attributes and methods from its immediate parent as well as ancestors.
  3. Avoid overly deep inheritance hierarchies to reduce complexity.

Real-Life Example: Academic Hierarchy

Here’s an example demonstrating a real-world hierarchy of academic roles:

#include <iostream>
using namespace std;

// Base Class
class Person {
protected:
    string name;

public:
    void setName(string n) {
        name = n;
    }
};

// Intermediate Derived Class
class Teacher : public Person {
protected:
    string subject;

public:
    void setSubject(string sub) {
        subject = sub;
    }
};

// Final Derived Class
class HeadOfDepartment : public Teacher {
private:
    string department;

public:
    void setDepartment(string dept) {
        department = dept;
    }

    void displayDetails() {
        cout << "Name: " << name << ", Subject: " << subject << ", Department: " << department << endl;
    }
};

int main() {
    HeadOfDepartment hod;

    hod.setName("Dr. Smith");
    hod.setSubject("Physics");
    hod.setDepartment("Science");

    hod.displayDetails();

    return 0;
}

Output:

Name: Dr. Smith, Subject: Physics, Department: Science  

Advantages of Multilevel Inheritance

  1. Logical Organization: Groups related classes into a hierarchy.
  2. Specialization: Each class can specialize and add unique functionality.
  3. Code Efficiency: Reduces duplication by sharing methods across multiple classes.

Potential Issues with Multilevel Inheritance

  1. Increased Complexity: Deep hierarchies can make debugging and maintenance harder.
  2. Diamond Problem: When combining multiple inheritance with multilevel inheritance, ambiguity may arise.

Explore More at The Coding College

Learn more about inheritance, OOP principles, and advanced programming topics at The Coding College.

Leave a Comment