Unit_12: Multi Inheritance Flashcards

1
Q

What is the main issue with multiple inheritance in C++?

A

Ambiguity — the child class may inherit methods with the same name from multiple parents, and it must disambiguate which method to use.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can a programmer resolve ambiguity caused by multiple inheritance?

A

By using fully qualified names, like A::foo(), to specify exactly which parent class’s method should be called.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why is multiple inheritance considered awkward in practice?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the diamond problem in object-oriented programming?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why does class C in the diagram have two copies of class A’s data?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How does the diamond problem lead to ambiguity in a program?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is virtual inheritance used for in C++?

A

Virtual inheritance is used to prevent multiple “instances” of a base class when using multiple inheritance, helping to resolve the diamond problem.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

A

TA has only one name because NamedPerson is inherited virtually, preventing duplication.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why does the TA object end up with two different number values?

A

Because both Employee and Student inherit NumberedPerson using regular (non-virtual) inheritance, so TA ends up with two separate copies of number.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly