Advertisement
RobertDeMilo

YB5.1 Введение в наследование

Nov 22nd, 2023
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.65 KB | None | 0 0
  1. //struct Apple
  2. //{
  3. //  int health = 10;
  4. //};
  5. //
  6. //struct Orange
  7. //{
  8. //  int health = 5;
  9. //};
  10. //
  11. //class Cat
  12. //{
  13. //public:
  14. //
  15. //  void Meow() const
  16. //  {
  17. //      cout << "meow! " << endl;
  18. //  }
  19. //  void Eat(const Apple& a)
  20. //  {
  21. //      cout << "Cat eats apple. " << a.health << "hp." << endl;
  22. //  }
  23. //  void Eat(const Orange& o)
  24. //  {
  25. //      cout << "Cat eats orange. " << o.health << "hp." << endl;
  26. //  }
  27. //};
  28. //
  29. //class Dog
  30. //{
  31. //public:
  32. //
  33. //  void Eat(const Apple& a)
  34. //  {
  35. //      cout << "Dog eats apple. " << a.health << "hp." << endl;
  36. //  }
  37. //  void Eat(const Orange& o)
  38. //  {
  39. //      cout << "Dog eats orange. " << o.health << "hp." << endl;
  40. //  }
  41. //};
  42. //------------------------------------------------------------------------------------
  43. //struct Fruit
  44. //{
  45. //  int health = 0;
  46. //  string type = "fruit";
  47. //
  48. //};
  49. //struct Apple : public Fruit
  50. //{
  51. //  Apple()
  52. //  {
  53. //      health = 10;
  54. //      type = "apple";
  55. //  }
  56. //};
  57. //
  58. //struct Orange : public Fruit
  59. //{
  60. //  Orange()
  61. //  {
  62. //      health = 5;
  63. //      type = "orange";
  64. //  }
  65. //};
  66. //
  67. //struct PineApple : public Fruit
  68. //{
  69. //  PineApple()
  70. //  {
  71. //      health = 15;
  72. //      type = "pineapple";
  73. //  }
  74. //};
  75. //
  76. //class Cat
  77. //{
  78. //public:
  79. //
  80. //  void Meow() const
  81. //  {
  82. //      cout << "meow! " << endl;
  83. //  }
  84. //  void Eat(const Fruit& f)
  85. //  {
  86. //      cout << "Cat eats " << f.type << ". " << f.health << "hp." << endl;
  87. //  }
  88. //
  89. //};
  90. //
  91. //class Dog
  92. //{
  93. //public:
  94. //
  95. //  void Eat(const Fruit& f)
  96. //  {
  97. //      cout << "Cat eats " << f.type << ". " << f.health << "hp." << endl;
  98. //  }
  99. //
  100. //};
  101. //------------------------------------------------------------------------------------
  102. #include <iostream>
  103.  
  104. using namespace std;
  105.  
  106. struct Fruit
  107. {
  108.     int health = 0;
  109.     string type = "fruit";
  110. };
  111.  
  112. struct Apple : public Fruit
  113. {
  114.     Apple()
  115.     {
  116.         health = 10;
  117.         type = "apple";
  118.     }
  119. };
  120.  
  121. struct Orange : public Fruit
  122. {
  123.     Orange()
  124.     {
  125.         health = 5;
  126.         type = "orange";
  127.     }
  128. };
  129.  
  130. struct PineApple : public Fruit
  131. {
  132.     PineApple()
  133.     {
  134.         health = 15;
  135.         type = "pineapple";
  136.     }
  137. };
  138.  
  139. class Animal
  140. {
  141. public:
  142.     void Eat(const Fruit& f)
  143.     {
  144.         cout << type << " eats " << f.type << ". " << f.health << "hp." << endl;
  145.     }
  146.  
  147.     string type = "animal";
  148. };
  149.  
  150. class Cat : public Animal
  151. {
  152. public:
  153.  
  154.     Cat()
  155.     {
  156.         type = "cat";
  157.     }
  158.  
  159.     void Meow() const
  160.     {
  161.         cout << "meow! " << endl;
  162.     }
  163. };
  164.  
  165. class Dog : public Animal
  166. {
  167. public:
  168.  
  169.     Dog()
  170.     {
  171.         type = "dog";
  172.     }
  173. };
  174.  
  175. void DoMeal(Animal& a, Fruit& f)
  176. {
  177.     a.Eat(f);
  178.     a.type += "*";
  179. }
  180.  
  181. int main()
  182. {
  183.     /*Cat c;
  184.     c.Meow();
  185.  
  186.     Apple a;
  187.     c.Eat(a);
  188.  
  189.     Orange o;
  190.     c.Eat(o);*/
  191.  
  192.     Dog d;
  193.     /*d.Eat(a);*/
  194.  
  195.     PineApple p;
  196.  
  197.     DoMeal(d, p);
  198.     DoMeal(d, p);
  199.     DoMeal(d, p);
  200.    
  201.     return 0;
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement