Downcasting With static_cast
mediumQuestion 446A static_cast can 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 class Animal has a virtual string name() const returning "animal".
Derived class Dog overrides name() to return "dog" AND adds a Dog-only method string fetch() const returning "fetching".
The runner holds an Animal* that actually points to a Dog, downcasts it with static_cast<Dog*>, and calls fetch(). 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, so 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.