C++ allows multiple inheritances. Multiple inheritances allow a child class to inherit from more than one parent class. The diamond problem occurs when two superclasses of a class have a common base class. For example, in the following diagram, the “D class” gets two copies of all attributes of “A class”, which causes ambiguities. Let see the below image which shows what happens without virtual inheritance?
A A
| |
B C
\ /
D
The solution to this problem is the ‘virtual’ keyword. We make the classes “B” and “C” as virtual base classes to avoid two copies of class “A” in the “D” class.
Answer:
C++ allows multiple inheritances. Multiple inheritances allow a child class to inherit from more than one parent class. The diamond problem occurs when two superclasses of a class have a common base class. For example, in the following diagram, the “D class” gets two copies of all attributes of “A class”, which causes ambiguities. Let see the below image which shows what happens without virtual inheritance?
The solution to this problem is the ‘virtual’ keyword. We make the classes “B” and “C” as virtual base classes to avoid two copies of class “A” in the “D” class.