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 rollno, marks and another base class is employee having data members name, age and designation. These both base classes are inherited from a single base class person with 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 rollno, marks and another base class is employee having data members name, age and designation. These both base classes are inherited from a single base class person with 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, ambiguity arises when two or more base classes have member functions or data members with the same name. This can lead to confusion and errors when accessing these members in the derived class.

For example, consider the following code:

class A {
public:
  int x;
};

class B {
public:
  int x;
};

class C : public A, public B {
public:
  void printX() {
    std::cout << "The value of x is: " << x << std::endl;
  }
};

In this example, the class C inherits the data member x from both classes A and B. When we call the function printX() on an object of class C, it is unclear which value of x should be printed.

Resolving Ambiguity in Multiple Inheritance

There are two main ways to resolve ambiguity in multiple inheritance:

  1. Using scope resolution operator (::): When a member function or data member is inherited from multiple base classes, we can use the scope resolution operator (::) to specify which base class's member we want to access.

  2. Using virtual inheritance: Virtual inheritance allows us to share a common base class among multiple derived classes without causing ambiguity. When a class is declared as virtual in a base class list, its members are not inherited directly into the derived class. Instead, a pointer to the virtual base class is inherited.

Program in C++

Here is a C++ program that demonstrates multiple inheritance and resolves ambiguity using the scope resolution operator:

#include 

class Person {
public:
  std::string name;
  int age;
};

class Employee : public Person {
public:
  std::string designation;
};

class Student : public Person {
public:
  int rollno;
  float marks;
};

class StudentEmployee : public Employee, public Student {
public:
  int employeeId;

  // Resolving ambiguity using the scope resolution operator
  void printName() {
    std::cout << "Name (from Person): " << Person::name << std::endl;
    std::cout << "Name (from Employee): " << Employee::name << std::endl;
  }

  void printDetails() {
    std::cout << "Name: " << name << std::endl;
    std::cout << "Age: " << age << std::endl;
    std::cout << "Designation: " << designation << std::endl;
    std::cout << "Roll Number: " << rollno << std::endl;
    std::cout << "Marks: " << marks << std::endl;
    std::cout << "Employee ID: " << employeeId << std::endl;
  }
};

int main() {
  StudentEmployee se;

  se.name = "John Doe";
  se.age = 21;
  se.designation = "Software Engineer";
  se.rollno = 123456;
  se.marks = 85.5;
  se.employeeId = 1001;

  se.printName();
  std::cout << std::endl;
  se.printDetails();

  return 0;
}

Output:

Name (from Person): John Doe
Name (from Employee): John Doe

Name: John Doe
Age: 21
Designation: Software Engineer
Roll Number: 123456
Marks: 85.5
Employee ID: 1001