1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. What happens when a derived object is passed by value to a…

Added 30 Nov -0001

What happens when a derived object is passed by value to a function taking a base class parameter in C++?

  1. A compile error reports the missing base-class conversion
  2. The derived destructor runs before the copy
  3. The derived part is sliced off, leaving a base copy Correct answer
  4. The whole derived object is copied intact

Correct answer

C. The derived part is sliced off, leaving a base copy

18 views

MCQSEXAM

Explanation

Passing by value constructs a base object from the derived one using the base copy constructor, so only the base subobject is copied and the derived members, along with virtual dispatch, are lost. This is object slicing, and it is why polymorphic functions should take references or pointers. Containers of base objects show the same problem, so containers of pointers or smart pointers are used instead.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is C? 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