CJCoding With Joseph

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:
meow
base

Do NOT write a main function.

Expected Output:

meow
base
Topics:
Inheritance
Code Editor
1
Tab to indent ยท Ctrl+Enter to run ยท 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.