What is virtual inheritance in C++?

2022

What is virtual inheritance in C++?

  1. A.

    C++ technique to enhance multiple inheritance.

  2. B.

    C++ technique to ensure that a private member of base class can be accessed.

  3. C.

    To avoid multiple inheritance of classes.

  4. D.

    To avoid multiple copies of the base class in derived class.

Attempted by 235 students.

Show answer & explanation

Correct answer: D

Virtual inheritance ensures that only one shared instance of a common base class exists when using multiple inheritance—this prevents duplicate base-class subobjects in the diamond problem. Problem: If class B and class C both inherit from class A, and class D inherits from both B and C, then without virtual inheritance D will contain two separate A subobjects (diamond problem).

Explore the full course: Uppsc Polytechnic Lecturer 2025 Cs