1.
What happens if both Person and Employee had a method with the same
name? How would you resolve ambiguity?
If both base classes have a method with the same name, like show(), then the
derived class Teacher will not know which one to call automatically.
This causes ambiguity.
To fix this, we specify the class name when calling the function:
Person::show();
Employee::show();
This way, we tell the compiler which class's function to use.
2. What are the potential issues with the diamond problem in multiple
inheritance? How can virtual inheritance help?
The diamond problem happens when two classes inherit from the same base
class, and a third class inherits from both of them.
So if:
• Person is a base class
• Employee and Student both inherit from Person
• Teacher inherits from both Employee and Student
Now Teacher has two copies of Person. This can cause confusion and data
duplication.
To solve this, we use virtual inheritance like this:
class Person {
};
class Employee : virtual public Person {
};
class Student : virtual public Person {
};
Now Teacher will only have one shared copy of Person, fixing the diamond
problem.