Unit_12: Multi Inheritance Flashcards
What is the main issue with multiple inheritance in C++?
Ambiguity — the child class may inherit methods with the same name from multiple parents, and it must disambiguate which method to use.
How can a programmer resolve ambiguity caused by multiple inheritance?
By using fully qualified names, like A::foo(), to specify exactly which parent class’s method should be called.
Why is multiple inheritance considered awkward in practice?
Because the programmer must remember and track which method comes from which parent, potentially many levels up the hierarchy. It increases complexity and the chance of errors.
What is the diamond problem in object-oriented programming?
diamond problem occurs in multiple inheritance when a class inherits from two classes that both inherit from a common base class, causing ambiguity due to duplicated members from the base class.
Why does class C in the diagram have two copies of class A’s data?
Because both classes B and C inherit from A separately, and class D inherits from both B and C, causing it to receive two separate copies of A’s members—one from each path.
How does the diamond problem lead to ambiguity in a program?
It creates confusion about which inherited member (from class A) should be used when the subclass (like D) tries to access it, leading to potential errors or unexpected behavior.
What is virtual inheritance used for in C++?
Virtual inheritance is used to prevent multiple “instances” of a base class when using multiple inheritance, helping to resolve the diamond problem.
In the diagram, how many name variables does TA have and why?
NamedPerson. -> ( string name)
|
| virtual inheritance
v
NumberedPerson -> (int number)
/ \
/ \
/ “Regular” \
/ inheritance \
v v
Employee Student
\ /
\ /
v v
TA
TA has only one name because NamedPerson is inherited virtually, preventing duplication.
Why does the TA object end up with two different number values?
Because both Employee and Student inherit NumberedPerson using regular (non-virtual) inheritance, so TA ends up with two separate copies of number.