โ Back to Inheritance
Question 140
Polymorphism Through Base Pointer
Polymorphism Through Base Pointer
You are given a base class Animal with a virtual function speak() that prints "..." and a virtual destructor. Create two derived classes: 1. Dog - publicly inherits from Animal, overrides speak() to print "Woof" 2. Cat - publicly inherits from Animal, overrides speak() to print "Meow" Both classes need constructors and properly overridden speak() 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
Expected Output:
Woof Meow
Topics:
ClassesInheritance
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.