CJCoding With Joseph

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

Do NOT write a main function.

Expected Output:

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