Question:

What is the main purpose of declaring a base class as virtual in multiple inheritance?

Show Hint

Virtual inheritance solves the diamond problem by keeping a single shared base subobject.
Updated On: Jul 2, 2026
  • To enforce pure virtual functions
  • To ensure only one copy of the base class exists
  • To prevent overriding of base class methods
  • To improve performance by reducing memory usage
Show Solution
collegedunia
Verified By Collegedunia

The Correct Option is B

Solution and Explanation

Step 1: Consider the diamond problem in C++. A class D inherits from B and C, and both B and C inherit from the same base class A. Without care, D would end up containing two separate copies of A.

Step 2: Two copies cause ambiguity: a reference to an A member from D does not know which copy to use, and it wastes storage.

Step 3: Declaring A as a virtual base class (using virtual inheritance in B and C) tells the compiler to keep just one shared copy of A inside D.

Step 4: This single shared subobject removes the ambiguity and makes member access unambiguous. So the main purpose is to ensure only one copy of the base class exists, which is option (B). The other options describe unrelated features.
Was this answer helpful?
0
0