When does ambiguity arise in multiple inheritance? How can one resolve it? Develop a program in C++ to create a derived class having the following items, name, age, rollno, marks, employee id and designation. Design a base class having data members as rollno, marks and another base class is employee having data members name and age. The program should carry out the required input and output(member function for all).


Q.) When does ambiguity arise in multiple inheritance? How can one resolve it? Develop a program in C++ to create a derived class having the following items, name, age, rollno, marks, employee id and designation. Design a base class having data members as rollno, marks and another base class is employee having data members name and age. The program should carry out the required input and output(member function for all).

Subject: Object Oriented Programming and Methodology

Ambiguity in Multiple Inheritance:

In multiple inheritance, when a derived class inherits from two or more base classes, and the base classes have the same member (such as a function or data member) with the same name, ambiguity arises. This is because the compiler becomes confused about which base class's member to inherit.

Resolving Ambiguity in Multiple Inheritance:

Several techniques can be used to resolve ambiguity in multiple inheritance:

  1. Using Scope Resolution Operator:

    • To specify the base class from which a particular member is inherited, use the scope resolution operator (::).
    • For example: class Derived : public Base1, public Base2 {
    • public:
    • int func() {
    • return Base1::func(); // calls func() in Base1
    • }
    • };
  2. Using Virtual Base Classes:

    • Declare the problematic base class as a virtual base class.
    • This ensures that each derived class object contains only one instance of the base class's members, eliminating ambiguity.
    • For example: class Base : virtual public Base1, public Base2 {
    • // members
    • };
  3. Using Member Function Overloading:

    • Overloading the conflicting member function in the derived class allows it to have multiple functions with the same name but different parameters.
    • The compiler can then determine the correct function to call based on the arguments provided.
  4. Using Public/Protected Inheritance:

    • If the ambiguity is caused by public inheritance from multiple base classes, consider changing the inheritance to protected or private.
    • This limits the accessibility of the conflicting members, reducing the likelihood of ambiguity.

C++ Program:

#include 

using namespace std;

// Base class Employee
class Employee {
protected:
    string name;
    int age;

public:
    void getEmployeeDetails() {
        cout << "Enter employee name: ";
        cin >> name;
        cout << "Enter employee age: ";
        cin >> age;
    }

    void displayEmployeeDetails() {
        cout << "Employee Name: " << name << endl;
        cout << "Employee Age: " << age << endl;
    }
};

// Base class Student
class Student {
protected:
    int rollno;
    int marks;

public:
    void getStudentDetails() {
        cout << "Enter student roll number: ";
        cin >> rollno;
        cout << "Enter student marks: ";
        cin >> marks;
    }

    void displayStudentDetails() {
        cout << "Student Roll Number: " << rollno << endl;
        cout << "Student Marks: " << marks << endl;
    }
};

// Derived class DerivedClass
class DerivedClass : public Employee, public Student {
private:
    int employeeId;
    string designation;

public:
    void getDerivedClassDetails() {
        getEmployeeDetails();  // Inherited from Employee
        getStudentDetails();  // Inherited from Student
        cout << "Enter employee ID: ";
        cin >> employeeId;
        cout << "Enter employee designation: ";
        cin >> designation;
    }

    void displayDerivedClassDetails() {
        displayEmployeeDetails();  // Inherited from Employee
        displayStudentDetails();  // Inherited from Student
        cout << "Employee ID: " << employeeId << endl;
        cout << "Employee Designation: " << designation << endl;
    }
};

int main() {
    DerivedClass obj;
    obj.getDerivedClassDetails();
    obj.displayDerivedClassDetails();

    return 0;
}

This program demonstrates multiple inheritance by creating a derived class DerivedClass that inherits from both Employee and Student. It includes member functions to input and display the details of an employee and a student. The ambiguity that might arise from inheriting the same member name from multiple base classes is resolved using the scope resolution operator (::).