C++ Multiple Inheritance

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 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 Academic class is defined with a grade attribute and a constructor to initialize it.
  • The displayAcademic method in the Academic class prints the academic details.
  • The Student class inherits publicly from both Person and Academic classes.
  • The Student constructor initializes the attributes of both base classes using member initializer lists.
  • The displayStudent method in the Student class calls the displayPerson and displayAcademic methods to print the combined details.
  • An object of the Student class is created in the main function, and its details are displayed using the displayStudent 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 and Academic classes both have a display method.
  • The Student class inherits from both Person and Academic.
  • The displayBoth method in the Student class resolves the ambiguity by explicitly specifying which display method to call using the scope resolution operator.
  • The main function creates an object of the Student class and calls the displayBoth 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 a name attribute and a display method.
  • The Student and Employee classes both inherit from Person using virtual inheritance to avoid duplicate inheritance of Person members.
  • The PartTime class inherits from both Student and Employee.
  • The PartTime constructor initializes the name attribute using the Person constructor.
  • The main function creates an object of the PartTime class and calls the display 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.

Leave a Comment

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

Scroll to Top