CJCoding With Joseph
15free / daySign inCreate account

Object Slicing Demonstration

Copying a derived object into a base OBJECT (by value) slices off the derived part, and virtual dispatch no longer reaches the override. Pointers and references do NOT slice.

Base class Animal has a virtual string sound() const returning "base".
Derived class Cat overrides sound() to return "meow".

The runner makes a Cat, binds an Animal& to it (no slicing), and also copies it into an Animal value (slicing). It prints the reference call then the sliced-value call (see Expected Output).

Do NOT write a main function.

Expected Output:

meow
base

= newline. The output ends with a newline — print one after the last line.

Hints2 available
Topics:
Inheritance
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.