/* * This code is written all in one file for instructional simplicity only; you should always split * your code into separate header files */ #include #include using namespace std; class Animal { public: void describe(); bool isAlive(); Animal(bool isAlive); ~Animal(); private: bool isAlive_; }; class Monkey : public Animal { public: void describe(); Monkey(); ~Monkey(); }; class Parrot : public Animal { public: void describe(); Parrot(); ~Parrot(); }; Animal::Animal(bool isAlive) : isAlive_(isAlive) {} Animal::~Animal() { cout << "cleaning cage" << endl; } bool Animal::isAlive() { return isAlive_; } Monkey::Monkey() {} Monkey::~Monkey() { cout << "releasing tree branches" << endl; } void Monkey::describe() { cout << "A monkey!" << endl; } Parrot::Parrot() {} Parrot::~Parrot() { cout << "Flying away" << endl; } void Parrot::describe() { cout << "A many-colored parrot" << endl; } int main() { list l; l.push_back(new Animal(true)); l.push_back(new Monkey()); l.push_back(new Parrot()); list::iterator it; for (it = l.begin(); it != l.end(); it++) { (*it)->describe(); cout << "This animal is alive: " << (*it)->isAlive() << endl; } return 0; }