CJCoding With Joseph

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:
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.