← Back to Inheritance
Question 452
Covariant Return Types With Clone
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 aclone()method. Create a polymorphic base classAnimal(virtual destructor; virtualstring kind() const-> "animal"; virtualAnimal* clone() constreturningnew Animal(*this)). CreateDogthat overrideskind()-> "dog" and overridesclone()to returnDog*(a covariant return) vianew Dog(*this). The runner clones aDogboth through anAnimal*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
Sign in to use hintsTopics:
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.