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
Personclass is defined withnameandageattributes and a constructor to initialize them. - The
displayPersonmethod in thePersonclass prints the details of the person. - The
Studentclass inherits from thePersonclass and adds an additional attribute,grade. It has a constructor to initialize all attributes, including those inherited from thePersonclass. ThedisplayStudentmethod calls thedisplayPersonmethod and then prints the grade. - The
GraduateStudentclass inherits from theStudentclass and adds an additional attribute,researchTopic. It has a constructor to initialize all attributes, including those inherited from theStudentclass. ThedisplayGraduateStudentmethod calls thedisplayStudentmethod and then prints the research topic. - An object of the
GraduateStudentclass is created in themainfunction, and its details are displayed using thedisplayGraduateStudentmethod.
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
Employeeclass is the base class withnameandemployeeIDattributes and a constructor to initialize them. ThedisplayEmployeemethod prints the employee’s details. - The
Managerclass inherits from theEmployeeclass and adds an additional attribute,teamSize. It has a constructor to initialize all attributes, including those inherited from theEmployeeclass. ThedisplayManagermethod calls thedisplayEmployeemethod and then prints the team size. - The
Directorclass inherits from theManagerclass and adds an additional attribute,budget. It has a constructor to initialize all attributes, including those inherited from theManagerclass. ThedisplayDirectormethod calls thedisplayManagermethod and then prints the budget. - An object of the
Directorclass is created in themainfunction, and its details are displayed using thedisplayDirectormethod.
Advantages of Multilevel Inheritance
- Code Reusability: Derived classes reuse the attributes and methods of the base class, reducing code duplication.
- Hierarchical Classification: Allows for a clear hierarchical classification of classes, making the code easier to understand and manage.
- 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.