Implement a program to demonstrate multiple inheritance in C++
🔷 Understanding Multiple Inheritance in C++
In object-oriented programming (OOP), inheritance allows a class to inherit attributes and behaviors (i.e., data and functions) from another class. One special and powerful form of inheritance in C++ is Multiple Inheritance.
✅ What is Multiple Inheritance?
Multiple inheritance means a single derived class can inherit from two or more base classes. This allows the derived class to combine and utilize features from multiple parent classes.
> Example: A class `Manager` may inherit from both `Employee` and `Department`.
🔑 Advantages of Multiple Inheritance
- Code Reusability: Inherit functions and data from more than one base class.
- Combining Functionalities: You can design classes with combined traits or roles.
- Flexibility: Offers more flexible modeling of real-world scenarios
⚠️ Challenges in Multiple Inheritance
- Ambiguity: If two base classes have a function or variable with the same name, the derived class must resolve which one to use.
- Complexity: Managing constructors and data flow can be complicated in large-scale projects.
🧑💻 C++ Program to Demonstrate Multiple Inheritance
Let’s create a program where a `Person` class and a `Qualification` class are both inherited by the `Employee` class.
#include <iostream>
using namespace std;
// Base Class 1
class Person {
protected:
string name;
int age;
public:
void setPersonDetails(string n, int a) {
name = n;
age = a;
}
void showPersonDetails() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
// Base Class 2
class Qualification {
protected:
string degree;
int year;
public:
void setQualification(string d, int y) {
degree = d;
year = y;
}
void showQualification() {
cout << "Degree: " << degree << endl;
cout << "Passing Year: " << year << endl;
}
};
// Derived Class
class Employee : public Person, public Qualification {
private:
int empID;
string department;
public:
void setEmployeeDetails(int id, string dept) {
empID = id;
department = dept;
}
void showEmployeeDetails() {
showPersonDetails(); // From Person
showQualification(); // From Qualification
cout << "Employee ID: " << empID << endl;
cout << "Department: " << department << endl;
}
};
int main() {
Employee e1;
// Setting values through functions from multiple base classes
e1.setPersonDetails("Vivek Bhardwaj", 30);
e1.setQualification("MCA", 2020);
e1.setEmployeeDetails(101, "Computer Science");
cout << "\nEmployee Profile:\n";
e1.showEmployeeDetails();
return 0;
}
🖨️ Sample Output
Employee Profile:
Name: Vivek Bhardwaj
Age: 30
Degree: MCA
Passing Year: 2020
Employee ID: 101
Department: Computer Science
📘 Code Explanation
- `Person` and `Qualification` are base classes.
- `Employee` is the derived class that inherits from both.
- The derived class combines personal, educational, and employment data.
- Functions from both base classes are accessible in the derived class.
📝 Conclusion
Multiple inheritance is a powerful feature in C++ that allows a class to inherit characteristics from more than one base class. However, it must be used wisely to avoid ambiguity and maintain clean code architecture.
Comments
Post a Comment