CJCoding With Joseph
15free / daySign inCreate account

dynamic_cast safely converts a base pointer to a derived pointer at RUNTIME: it returns a valid pointer if the object really is that type, or nullptr if it is not. The base must be polymorphic (have at least one virtual function).

Create a polymorphic base class Animal (give it a virtual destructor and a virtual string kind() const returning "animal"). Create Dog (overrides kind() -> "dog", adds string bark() const -> "woof") and Cat (overrides kind() -> "cat").

The runner has an Animal* pointing to a Dog. dynamic_cast<Dog*> succeeds (prints "woof"); dynamic_cast<Cat*> returns nullptr (prints "not a cat"). Output the output shown in Expected Output.

Do NOT write a main function.

Expected output

woof
not a cat

= newline. The output ends with a newline, so print one after the last line.

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