What is the purpose of virtual inheritance in C++?

Options

  • A. To prevent all inheritance
  • B. To help avoid duplicate base-class subobjects in a diamond inheritance structure
  • C. To make every member function virtual automatically
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

B. To help avoid duplicate base-class subobjects in a diamond inheritance structure

Detailed Explanation

Correct Answer: Option B

Explanation:
Virtual inheritance helps ensure that a most-derived object contains one shared subobject of a common virtual base class in a diamond-shaped inheritance hierarchy. For example, classes B and C can both virtually inherit from A, while D inherits from B and C.