aydarbiktimirov

Untitled

Jan 24th, 2012
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. class animal
  5. {
  6.     public:
  7.         virtual std::string say() const = 0;
  8. };
  9.  
  10. class cat:
  11.     public animal
  12. {
  13.     public:
  14.         virtual std::string say() const
  15.         {
  16.             return "meow";
  17.         }
  18. };
  19.  
  20. class dog:
  21.     public animal
  22. {
  23.     public:
  24.         virtual std::string say() const
  25.         {
  26.             return "bark";
  27.         }
  28. };
  29.  
  30. class animal_container
  31. {
  32.     public:
  33.         void check(const animal &a) const
  34.         {
  35.             std::cout << a.say() << std::endl;
  36.         }
  37. };
  38.  
  39. int main()
  40. {
  41.     animal_container a;
  42.     cat c;
  43.     dog d;
  44.     a.check(c);
  45.     a.check(d);
  46.     a.check(d);
  47.     a.check(c);
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment