Question:medium

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

The Correct Option is B

Solution and Explanation

Step 1: Picture a diamond hierarchy where two parent classes both derive from a common grandparent. Normal inheritance duplicates that grandparent in the most derived class.

Step 2: That duplication is the problem: the grandparent's data appears twice, and calls to its members become ambiguous.

Step 3: Marking the common base as virtual instructs the compiler to merge those would-be duplicates into a single shared subobject.

Step 4: With one shared copy, the ambiguity disappears and the layout is clean. Enforcing pure virtuals, blocking overrides, and raw performance are not the goal here.

\[\boxed{\text{Only one copy of the base class exists}}\]
Was this answer helpful?
0