← 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 the output shown in Expected Output. Do NOT write a main function.
Expected Output:
Hello= 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.