Skip to main content
C++ Bug Hunt Hard +35 XP

Slicing Problem

Derived class data is lost during assignment.

C++Find the bug
1class Animal { public: virtual string speak() = 0; };
2class Dog : public Animal { string speak() { return "Woof"; } };
3Animal a = Dog(); // slicing!
The bug is on line 3

Select the correct fix

Show answer and explanation

Answer: Use Animal* or Animal& (pointer/reference)

Object slicing occurs when a derived object is assigned to a base object by value — the derived data is sliced off. Always use pointers or references for polymorphism.

Hint: Polymorphism requires pointers or references, not values.

What this challenge tests

Bug Hunt in C++: slicing, polymorphism, inheritance.

Related challenges