Write a program to implement multilevel inheritance in C++
🔷 Implementing Multilevel Inheritance in C++
Inheritance is a powerful concept in object-oriented programming that allows us to create a hierarchy of classes. One of the common and useful forms of inheritance in C++ is Multilevel Inheritance.
✅ What is Multilevel Inheritance?
Multilevel inheritance refers to a scenario where a class is derived from another derived class. This creates a chain of inheritance where:
- Class B inherits from Class A
- Class C inherits from Class B
This structure helps in extending the functionality and reusability of code over multiple levels of class hierarchy.
> 📌 Example: `Person → Employee → Manager`
🧠 Why Use Multilevel Inheritance?
- To model real-world hierarchical relationships.
- To reuse code across multiple levels of classes.
- To extend the functionalities of base classes in a structured manner.
🧑💻 C++ Program to Demonstrate Multilevel Inheritance
Let’s implement a simple program using three classes:
- `Person` – Base class (Level 1)
- `Employee` – Derived from `Person` (Level 2)
- `Manager` – Derived from `Employee` (Level 3)
#include <iostream>
using namespace std;
// Base Class - Level 1
class Person {
protected:
string name;
int age;
public:
void setPerson(string n, int a) {
name = n;
age = a;
}
void showPerson() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
// Derived Class - Level 2
class Employee : public Person {
protected:
int empID;
string department;
public:
void setEmployee(int id, string dept) {
empID = id;
department = dept;
}
void showEmployee() {
cout << "Employee ID: " << empID << endl;
cout << "Department: " << department << endl;
}
};
// Derived Class - Level 3
class Manager : public Employee {
private:
int teamSize;
string level;
public:
void setManager(int tSize, string lvl) {
teamSize = tSize;
level = lvl;
}
void showManager() {
showPerson(); // From Person
showEmployee(); // From Employee
cout << "Team Size: " << teamSize << endl;
cout << "Manager Level: " << level << endl;
}
};
int main() {
Manager m1;
m1.setPerson("Vivek Bhardwaj", 30);
m1.setEmployee(101, "IT");
m1.setManager(5, "Senior Manager");
cout << "Manager Profile:\n";
m1.showManager();
return 0;
}
🖨️ Output
Manager Profile:
Name: Vivek Bhardwaj
Age: 30
Employee ID: 101
Department: IT
Team Size: 5
Manager Level: Senior Manager
📘 Explanation
- `Manager` inherits from `Employee`, which inherits from `Person`.
- All member functions and variables from `Person` and `Employee` are available to `Manager`.
- This multilevel structure models a real-world scenario where a manager is an employee, and an employee is a person.
📝 Conclusion
Multilevel inheritance in C++ provides a structured approach to model hierarchical relationships among classes. It promotes better code reuse, cleaner designs, and is essential in large-scale applications that involve real-world entities with tiered roles.
By understanding and applying multilevel inheritance, programmers can build flexible and extendable systems with minimal code redundancy.
Comments
Post a Comment