CJCoding With Joseph
15free / daySign inCreate account

Downcasting With static_cast

A 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 — print one after the last line.

Hints3 available
Topics:
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.