C++ Multilevel Inheritance

Introduction

Multilevel inheritance in C++ is a type of inheritance where a class is derived from another derived class. This creates a chain of inheritance, where a base class is extended by an intermediate class, which is further extended by another class. This hierarchy allows for the progressive extension of functionality and the reuse of code across multiple levels of the inheritance chain.

Example: Implementing Multilevel Inheritance

Let’s explore an example of multilevel inheritance where we have a base class, an intermediate derived class, and a final derived class.

Base Class: Person

The Person class will contain basic information about a person.

Intermediate Derived Class: Student

The Student class will inherit from Person and add attributes specific to a student.

Final Derived Class: GraduateStudent

The GraduateStudent class will inherit from Student and add additional attributes specific to a graduate student.

Code Example

#include <iostream>
using namespace std;

// Base class
class Person {
public:
    string name;
    int age;

    // Constructor to initialize name and age
    Person(string n, int a) : name(n), age(a) {}

    // Method to display person details
    void displayPerson() {
        cout << "Name: " << name << ", Age: " << age << endl;
    }
};

// Intermediate derived class
class Student : public Person {
public:
    double grade;

    // Constructor to initialize name, age, and grade
    Student(string n, int a, double g) : Person(n, a), grade(g) {}

    // Method to display student details
    void displayStudent() {
        displayPerson(); // Call base class method
        cout << "Grade: " << grade << endl;
    }
};

// Final derived class
class GraduateStudent : public Student {
public:
    string researchTopic;

    // Constructor to initialize name, age, grade, and research topic
    GraduateStudent(string n, int a, double g, string rt) : Student(n, a, g), researchTopic(rt) {}

    // Method to display graduate student details
    void displayGraduateStudent() {
        displayStudent(); // Call intermediate class method
        cout << "Research Topic: " << researchTopic << endl;
    }
};

int main() {
    // Create an object of the final derived class
    GraduateStudent gradStudent("Aarav", 25, 88.5, "Artificial Intelligence");

    // Display graduate student details
    gradStudent.displayGraduateStudent();

    return 0;
}

Output

Name: Aarav, Age: 25
Grade: 88.5
Research Topic: Artificial Intelligence

Explanation

  • The Person class is defined with name and age attributes and a constructor to initialize them.
  • The displayPerson method in the Person class prints the details of the person.
  • The Student class inherits from the Person class and adds an additional attribute, grade. It has a constructor to initialize all attributes, including those inherited from the Person class. The displayStudent method calls the displayPerson method and then prints the grade.
  • The GraduateStudent class inherits from the Student class and adds an additional attribute, researchTopic. It has a constructor to initialize all attributes, including those inherited from the Student class. The displayGraduateStudent method calls the displayStudent method and then prints the research topic.
  • An object of the GraduateStudent class is created in the main function, and its details are displayed using the displayGraduateStudent method.

Practical Examples

Example 1: Employee Inheritance

In this example, we have an Employee base class, a Manager intermediate derived class, and a Director final derived class.

#include <iostream>
using namespace std;

// Base class
class Employee {
public:
    string name;
    int employeeID;

    // Constructor to initialize name and employee ID
    Employee(string n, int id) : name(n), employeeID(id) {}

    // Method to display employee details
    void displayEmployee() {
        cout << "Name: " << name << ", Employee ID: " << employeeID << endl;
    }
};

// Intermediate derived class
class Manager : public Employee {
public:
    int teamSize;

    // Constructor to initialize name, employee ID, and team size
    Manager(string n, int id, int ts) : Employee(n, id), teamSize(ts) {}

    // Method to display manager details
    void displayManager() {
        displayEmployee(); // Call base class method
        cout << "Team Size: " << teamSize << endl;
    }
};

// Final derived class
class Director : public Manager {
public:
    double budget;

    // Constructor to initialize name, employee ID, team size, and budget
    Director(string n, int id, int ts, double b) : Manager(n, id, ts), budget(b) {}

    // Method to display director details
    void displayDirector() {
        displayManager(); // Call intermediate class method
        cout << "Budget: $" << budget << endl;
    }
};

int main() {
    // Create an object of the final derived class
    Director director("Siya", 101, 10, 150000.0);

    // Display director details
    director.displayDirector();

    return 0;
}

Output

Name: Siya, Employee ID: 101
Team Size: 10
Budget: $150000

Explanation

  • The Employee class is the base class with name and employeeID attributes and a constructor to initialize them. The displayEmployee method prints the employee’s details.
  • The Manager class inherits from the Employee class and adds an additional attribute, teamSize. It has a constructor to initialize all attributes, including those inherited from the Employee class. The displayManager method calls the displayEmployee method and then prints the team size.
  • The Director class inherits from the Manager class and adds an additional attribute, budget. It has a constructor to initialize all attributes, including those inherited from the Manager class. The displayDirector method calls the displayManager method and then prints the budget.
  • An object of the Director class is created in the main function, and its details are displayed using the displayDirector method.

Advantages of Multilevel Inheritance

  1. Code Reusability: Derived classes reuse the attributes and methods of the base class, reducing code duplication.
  2. Hierarchical Classification: Allows for a clear hierarchical classification of classes, making the code easier to understand and manage.
  3. Extensibility: New functionality can be added to the system by introducing new derived classes at various levels of the hierarchy.

Conclusion

Multilevel inheritance in C++ allows a class to be derived from another derived class, creating a chain of inheritance. This chapter covered the basics of implementing multilevel inheritance, with practical examples demonstrating its usage. Understanding multilevel inheritance is essential for writing modular and maintainable C++ code, enabling the creation of complex systems with clear and reusable class hierarchies.

Leave a Comment

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

Scroll to Top