Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. class animal
  2. {
  3. // members, constructors and other methods...
  4.  
  5. public:
  6. virtual void what_do_you_eat() { cout << "i eat generic food" << endl; }
  7. }
  8.  
  9. class cat : public animal
  10. {
  11. // members, constructors and other methods...
  12.  
  13. public:
  14. void what_do_you_eat() { cout << "i eat cat food" << endl; }
  15. }
  16.  
  17. class dog : public animal
  18. {
  19. // members, constructors and other methods...
  20.  
  21. public:
  22. void what_do_you_eat() { cout << "i eat dog food" << endl; }
  23. }
  24.  
  25. std::list<animal> animals
  26. animals.push_back( dog() );
  27. animals.push_back( cat() );
  28.  
  29. for(auto itr : animals)
  30. {
  31. itr.what_do_you_eat();
  32. }
  33. // output:
  34. // i eat generic food
  35. // i eat generic food
  36. // i eat generic food
  37. // i eat generic food
  38. // ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement