← Back to Inheritance
Question 446
Downcasting With static_cast
Downcasting With static_cast
Astatic_castcan convert a base pointer down to a derived pointer when you already know the real object type, letting you call methods that only exist on the derived class. Base classAnimalhas a virtualstring name() constreturning "animal". Derived classDogoverridesname()to return "dog" AND adds aDog-only methodstring fetch() constreturning "fetching". The runner holds anAnimal*that actually points to aDog, downcasts it withstatic_cast<Dog*>, and callsfetch(). The program prints the output shown in Expected Output. Do NOT write a main function.
Expected Output:
fetching= newline. The output ends with a newline — print one after the last line.
Hints3 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.