โ Back to Inheritance
Question 435
Polymorphism Through a Base Reference
Polymorphism Through a Base Reference
Virtual dispatch works through base REFERENCES, not only pointers. Base classSpeakerhas a virtual methodstring speak() constreturning "...". Create a derived classHumanthat overridesspeak()to return "Hello". The runner binds aSpeaker&reference to aHumanand callsspeak()through it. Becausespeak()is virtual, the program prints: Hello Do NOT write a main function.
Expected Output:
Hello
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.