โ 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: fetching Do NOT write a main function.
Expected Output:
fetching
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.