1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. class D : public B1, public B2, where both B1 and B2…

Added 30 Nov -0001

class D : public B1, public B2, where both B1 and B2 virtually inherit A. How many A subobjects does a D contain?

  1. It depends on the compiler, since merging of virtual bases is left implementation-defined
  2. Exactly one, shared by both paths, because virtual bases are merged into one subobject Correct answer
  3. Two, one reached through B1 and one through B2, addressed by distinct base offsets
  4. Zero, because virtual inheritance removes the base class entirely from the layout

Correct answer

B. Exactly one, shared by both paths, because virtual bases are merged into one subobject

16 views

MCQSEXAM

Explanation

Virtual inheritance makes the base a shared subobject, so the diamond stores one A that both B1 and B2 refer to, which removes the ambiguity of d.A::member. Construction of the virtual base is the responsibility of the most-derived class, so D initialises A directly. The runtime cost is indirect access through a base offset table rather than a fixed displacement.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is B? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C++ and Object-Oriented Programming MCQs

Practise all C++ and Object-Oriented Programming MCQs

Similar MCQs