Introduction
Multiple inheritance in C++ allows a derived class to inherit members (attributes and methods) from more than one base class. This can be useful for creating classes that combine the functionality of multiple base classes. However, it also introduces complexities such as ambiguity and the diamond problem, which need to be managed carefully.
Example: Implementing Multiple Inheritance
Let’s explore a simple example of multiple inheritance where a derived class inherits from two base classes.
Base Classes: Person
and Academic
The Person
class will contain basic information about a person. The Academic
class will contain information about academic achievements.
Derived Class: Student
The Student
class will inherit from both Person
and Academic
.
Code Example
#include <iostream>
using namespace std;
// Base class Person
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;
}
};
// Base class Academic
class Academic {
public:
double grade;
// Constructor to initialize grade
Academic(double g) : grade(g) {}
// Method to display academic details
void displayAcademic() {
cout << "Grade: " << grade << endl;
}
};
// Derived class Student inheriting from Person and Academic
class Student : public Person, public Academic {
public:
// Constructor to initialize all attributes
Student(string n, int a, double g) : Person(n, a), Academic(g) {}
// Method to display student details
void displayStudent() {
displayPerson(); // Call method from Person class
displayAcademic(); // Call method from Academic class
}
};
int main() {
// Create an object of the derived class
Student student("Aarav", 20, 88.5);
// Display student details
student.displayStudent();
return 0;
}
Output
Name: Aarav, Age: 20
Grade: 88.5
Explanation
- The
Person
class is defined withname
andage
attributes and a constructor to initialize them. - The
displayPerson
method in thePerson
class prints the details of the person. - The
Academic
class is defined with agrade
attribute and a constructor to initialize it. - The
displayAcademic
method in theAcademic
class prints the academic details. - The
Student
class inherits publicly from bothPerson
andAcademic
classes. - The
Student
constructor initializes the attributes of both base classes using member initializer lists. - The
displayStudent
method in theStudent
class calls thedisplayPerson
anddisplayAcademic
methods to print the combined details. - An object of the
Student
class is created in themain
function, and its details are displayed using thedisplayStudent
method.
Managing Ambiguity in Multiple Inheritance
When a derived class inherits from multiple base classes that have members with the same name, ambiguity can arise. This can be resolved using the scope resolution operator.
Example: Resolving Ambiguity
#include <iostream>
using namespace std;
// Base class Person
class Person {
public:
void display() {
cout << "Display from Person" << endl;
}
};
// Base class Academic
class Academic {
public:
void display() {
cout << "Display from Academic" << endl;
}
};
// Derived class Student inheriting from Person and Academic
class Student : public Person, public Academic {
public:
// Method to resolve ambiguity and call both display methods
void displayBoth() {
Person::display(); // Call display from Person
Academic::display(); // Call display from Academic
}
};
int main() {
// Create an object of the derived class
Student student;
// Display both messages
student.displayBoth();
return 0;
}
Output
Display from Person
Display from Academic
Explanation
- The
Person
andAcademic
classes both have adisplay
method. - The
Student
class inherits from bothPerson
andAcademic
. - The
displayBoth
method in theStudent
class resolves the ambiguity by explicitly specifying whichdisplay
method to call using the scope resolution operator. - The
main
function creates an object of theStudent
class and calls thedisplayBoth
method to display messages from both base classes.
The Diamond Problem
The diamond problem occurs in multiple inheritance when a derived class inherits from two classes that have a common base class. This can lead to ambiguity and duplicate inheritance of the base class members.
Example: Diamond Problem
#include <iostream>
using namespace std;
// Base class
class Person {
public:
string name;
// Constructor to initialize name
Person(string n) : name(n) {}
// Method to display name
void display() {
cout << "Name: " << name << endl;
}
};
// Intermediate base class Student inheriting from Person
class Student : virtual public Person {
public:
// Constructor to initialize name
Student(string n) : Person(n) {}
};
// Intermediate base class Employee inheriting from Person
class Employee : virtual public Person {
public:
// Constructor to initialize name
Employee(string n) : Person(n) {}
};
// Derived class PartTime inheriting from Student and Employee
class PartTime : public Student, public Employee {
public:
// Constructor to initialize name
PartTime(string n) : Person(n), Student(n), Employee(n) {}
};
int main() {
// Create an object of the derived class
PartTime partTimeWorker("Aarav");
// Display name
partTimeWorker.display();
return 0;
}
Output
Name: Aarav
Explanation
- The
Person
class is the common base class with aname
attribute and adisplay
method. - The
Student
andEmployee
classes both inherit fromPerson
usingvirtual
inheritance to avoid duplicate inheritance ofPerson
members. - The
PartTime
class inherits from bothStudent
andEmployee
. - The
PartTime
constructor initializes thename
attribute using thePerson
constructor. - The
main
function creates an object of thePartTime
class and calls thedisplay
method to display the name.
Conclusion
Multiple inheritance in C++ allows a derived class to inherit members from more than one base class, enabling the creation of classes that combine functionalities from multiple sources. This chapter covered the basics of implementing multiple inheritance, managing ambiguity, and handling the diamond problem using virtual inheritance. Understanding multiple inheritance and its complexities is crucial for writing robust and maintainable C++ code.