CJCoding With Joseph
15free / daySign inCreate account

Covariant Return Types With Clone

An overriding virtual function may return a pointer to a MORE derived type than the base declares. This is called a covariant return type and is commonly used for a clone() method.

Create a polymorphic base class Animal (virtual destructor; virtual string kind() const -> "animal"; virtual Animal* clone() const returning new Animal(*this)).
Create Dog that overrides kind() -> "dog" and overrides clone() to return Dog* (a covariant return) via new Dog(*this).

The runner clones a Dog both through an Animal* and directly, printing the copy's kind each time (see Expected Output).

Do NOT write a main function.

Expected Output:

dog
dog

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

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