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