Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class animal
- {
- // members, constructors and other methods...
- public:
- virtual void what_do_you_eat() { cout << "i eat generic food" << endl; }
- }
- class cat : public animal
- {
- // members, constructors and other methods...
- public:
- void what_do_you_eat() { cout << "i eat cat food" << endl; }
- }
- class dog : public animal
- {
- // members, constructors and other methods...
- public:
- void what_do_you_eat() { cout << "i eat dog food" << endl; }
- }
- std::list<animal> animals
- animals.push_back( dog() );
- animals.push_back( cat() );
- for(auto itr : animals)
- {
- itr.what_do_you_eat();
- }
- // output:
- // i eat generic food
- // i eat generic food
- // i eat generic food
- // i eat generic food
- // ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement