← Back to Classes
Question 140
Polymorphism Through Base Pointer
Polymorphism Through Base Pointer
You are given a base class Animal with a virtual functionspeak()that prints "..." and a virtual destructor. Create two derived classes: 1. Dog - publicly inherits from Animal, overridesspeak()to print "Woof" 2. Cat - publicly inherits from Animal, overridesspeak()to print "Meow" Both classes need constructors and properly overriddenspeak()functions. The test will create both objects through base class pointers to demonstrate polymorphism. Do NOT include a main function. Your code will be tested automatically.
Expected Output:
Woof Meow
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
ClassesInheritance
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.