#include #include using namespace std; class Animal { public: ~Animal() = default; virtual string name() const = 0; virtual string noise() const = 0; }; class Cat : public Animal { public: string name() const { return "cat"; } string noise() const { return "miow"; } }; class Dog : public Animal { public: string name() const { return "dog"; } string noise() const { return "woff"; } }; class Cow : public Animal { public: string name() const { return "cow"; } string noise() const { return "moo"; } }; void print_verse(Animal const* animal) { cout << "Old MacDonald had a farm, E-I-E-I-O," << endl << "And on that farm he had a " << animal->name() << ", E-I-E-I-O," << endl << "With a " << animal->noise() << " " << animal->noise() << " here and a " << animal->noise() << " " << animal->noise() << " there" << endl << "Here a " << animal->noise() << ", there a " << animal->noise() << ", everywhere a " << animal->noise() << " " << animal->noise() << endl << "Old MacDonald had a farm, E-I-E-I-O." << endl << endl; } int main() { // Create a vector with Animals vector v; // Insert at least three different animals in the vector v.push_back(new Cow); v.push_back(new Cat); v.push_back(new Dog); // Iterate the vector and call print_verse with each animal for ( Animal const* a : v ) print_verse(a); // Clean up any allocated memory for ( Animal const* a : v ) delete a; return 0; }