← Back to Inheritance
Question 447
Object Slicing Demonstration
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 classAnimalhas a virtualstring sound() constreturning "base". Derived classCatoverridessound()to return "meow". The runner makes aCat, binds anAnimal&to it (no slicing), and also copies it into anAnimalvalue (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
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.