Advertisement
RobertDeMilo

YB5.4 Унификация работы с классо специфичным кодом. Постановка проблемы

Apr 14th, 2024
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. //#include <iostream>
  2. //
  3. //using namespace std;
  4. //
  5. //class Animal
  6. //{
  7. //public:
  8. //
  9. //  Animal(const string& type):type_(type){}
  10. //
  11. //  void Eat(const string& fruit)
  12. //  {
  13. //      cout<<type_<<" eats "<<fruit<<endl;
  14. //  }
  15. //
  16. //private:
  17. //  const string type_;
  18. //};
  19. //
  20. //class Cat : public Animal
  21. //{
  22. //public:
  23. //
  24. //  Cat():Animal("cat"){}
  25. //
  26. //
  27. //  void Meow() const
  28. //  {
  29. //      cout << "Meow! " << endl;
  30. //  }
  31. //};
  32. //
  33. //class Dog : public Animal
  34. //{
  35. //public:
  36. //
  37. //  Dog():Animal("dog"){}
  38. //
  39. //  void Bark() const
  40. //  {
  41. //      cout << "Whaf! " << endl;
  42. //  }
  43. //};
  44. //
  45. //void MakeSound(const Cat& c)
  46. //{
  47. //  c.Meow();
  48. //}
  49. //
  50. //void MakeSound(const Dog& d)
  51. //{
  52. //  d.Bark();
  53. //}
  54. //
  55. //int main()
  56. //{
  57. //  Cat c;
  58. //  Dog d;
  59. //  c.Eat("apple");
  60. //  d.Eat("orange");
  61. //
  62. //  MakeSound(c);
  63. //  MakeSound(d);
  64. //
  65. //  return 0;
  66. //}
  67.  
  68.  
  69. Из базового класса мы не можем добраться до приватных полей классов потомков.
  70. Иначе это нарушало бы инкапсуляцию (т.е сокрытие данных) и детали внутренней реализации.
  71. #include <iostream>
  72.  
  73. using namespace std;
  74.  
  75. class Animal
  76. {
  77. public:
  78.  
  79.     Animal(const string& type):type_(type){}
  80.  
  81.     void Eat(const string& fruit)
  82.     {
  83.         cout<<type_<<" eats "<<fruit<<endl;
  84.     }
  85.    
  86.     void Voice() const
  87.     {
  88.         if(type_=="cat")
  89.         {
  90.             cout << "Meow! " << endl;
  91.         }
  92.         else if(type_=="dog")
  93.         {
  94.             cout << "Whaf! " << endl;
  95.         }
  96.         else if(type_=="parrot")
  97.         {
  98.             cout << name_<<" is good!"<<endl;
  99.         }
  100.     }
  101.  
  102. private:
  103.     const string type_;
  104. };
  105.  
  106. class Cat : public Animal
  107. {
  108. public:
  109.  
  110.     Cat():Animal("cat"){}
  111. };
  112.  
  113. class Dog : public Animal
  114. {
  115. public:
  116.  
  117.     Dog():Animal("dog"){}
  118. };
  119.  
  120. class Parrot:public Animal
  121. {
  122. public:
  123.    
  124.     Parrot(const string& name):Animal("parrot"),name_(name){}
  125.    
  126. //  void Talk()
  127. //  {
  128. //      cout<<name_<<" is good!"<<endl;
  129. //  }
  130. private:
  131.     const string& name_;
  132. };
  133.  
  134. void MakeSound(const Animal& a)
  135. {
  136.     a.Voice();
  137. }
  138.  
  139. int main()
  140. {
  141.     Cat c;
  142.     Dog d;
  143.    
  144.     c.Eat("apple");
  145.     d.Eat("orange");
  146.  
  147.     MakeSound(c);
  148.     MakeSound(d);
  149.  
  150.     return 0;
  151. }
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement