โ 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: 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.