Object Slicing Demonstration
mediumQuestion 447Copying 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, so print one after the last line.
Hints2 available
Sign in to use hintsTopics
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.