fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std; // Добавлено использование пространства имен std
  5.  
  6. class Animal {
  7. protected:
  8. string name;
  9. int age;
  10.  
  11. public:
  12. Animal(string n, int a);
  13. virtual void makeSound();
  14. void eat();
  15. virtual void sleep();
  16. };
  17.  
  18. class Mammal : protected Animal {
  19. protected:
  20. bool isSleeping; // Исправлено: добавлено поле isSleeping
  21.  
  22. public:
  23. Mammal(string n, int a);
  24. void makeSound() override; // Использование override для переопределения метода
  25. void eat();
  26. void sleep() override; // Использование override для переопределения метода
  27. };
  28.  
  29. class Dog : public Mammal {
  30. private:
  31. bool isTailWagging;
  32.  
  33. public:
  34. Dog(string n, int a);
  35. void makeSound() override; // Использование override для переопределения метода
  36. // void eat() override; // Использование override для переопределения метода
  37. void sleep() override; // Использование override для переопределения метода
  38. void fetch();
  39. void wagTail();
  40. };
  41.  
  42. class Cat : public Mammal {
  43. private:
  44. int numberOfLives;
  45.  
  46. public:
  47. Cat(string n, int a);
  48. void makeSound() override; // Использование override для переопределения метода
  49. // void eat() override; // Использование override для переопределения метода
  50. void sleep() override; // Использование override для переопределения метода
  51. };
  52.  
  53. class Tail : public Dog {
  54. public:
  55. Tail(string n, int a);
  56. };
  57.  
  58. // Реализация методов классов
  59.  
  60. Animal::Animal(string n, int a) {
  61. name = n;
  62. age = a;
  63. }
  64.  
  65. void Animal::makeSound() {
  66. cout << "This is a generic animal sound." << endl;
  67. }
  68.  
  69. void Animal::eat() {
  70. cout << "The animal is eating." << endl;
  71. }
  72.  
  73. void Animal::sleep() {
  74. cout << "The animal is sleeping." << endl;
  75. }
  76.  
  77. Mammal::Mammal(string n, int a) : Animal(n, a) {
  78. isSleeping = false;
  79. }
  80.  
  81. void Mammal::makeSound() {
  82. cout << "This is a generic mammal sound." << endl;
  83. }
  84.  
  85. void Mammal::eat() {
  86. cout << "The mammal is eating." << endl;
  87. }
  88.  
  89. void Mammal::sleep() {
  90. cout << "The mammal is sleeping." << endl;
  91. isSleeping = true;
  92. }
  93.  
  94. Dog::Dog(string n, int a) : Mammal(n, a) {
  95. isTailWagging = false;
  96. }
  97.  
  98. void Dog::makeSound() {
  99. cout << "Woof!" << endl;
  100. }
  101.  
  102. //void Dog::eat() {
  103. // cout << "The dog is eating." << endl;
  104. //}
  105.  
  106. void Dog::sleep() {
  107. cout << "The dog is sleeping." << endl;
  108. isSleeping = true;
  109. }
  110.  
  111. void Dog::fetch() {
  112. cout << "The dog is fetching." << endl;
  113. }
  114.  
  115. void Dog::wagTail() {
  116. if (isSleeping) {
  117. cout << "The dog can't wag its tail because it's sleeping." << endl;
  118. } else {
  119. cout << "The dog is wagging its tail." << endl;
  120. isTailWagging = true;
  121. }
  122. }
  123.  
  124. Cat::Cat(string n, int a) : Mammal(n, a) {
  125. numberOfLives = 9;
  126. }
  127.  
  128. void Cat::makeSound() {
  129. cout << "Meow!" << endl;
  130. }
  131.  
  132. //void Cat::eat() {
  133. // cout << "The cat is eating." << endl;
  134. //}
  135.  
  136. void Cat::sleep() {
  137. cout << "The cat is sleeping." << endl;
  138. isSleeping = true;
  139. }
  140.  
  141. Tail::Tail(string n, int a) : Dog(n, a) {} // Исправлено: передача параметров в базовый класс
  142.  
  143. int main() {
  144. Dog d("Fido", 3);
  145. Cat c("Fluffy", 5);
  146. Dog d1("Barky", 3);
  147.  
  148. Mammal *arr[] = {&d, &c, &d1};
  149.  
  150. // should woof, meow, woof
  151. for (int i = 0; i < 3; i++){
  152. arr[i]->makeSound();
  153. }
  154.  
  155. // should eat in dog, cat, dog order
  156. for (int i = 0; i < 3; i++){
  157. arr[i]->eat();
  158. }
  159.  
  160. // should sleep in dog, cat, dog order
  161. for (int i = 0; i < 3; i++){
  162. arr[i]->sleep();
  163. }
  164.  
  165. // shouldn't do anything as they're sleeping
  166. for (int i = 0; i < 3; i++){
  167. arr[i]->makeSound();
  168. }
  169.  
  170. // shouldn't eat actually, as they are sleeping
  171. for (int i = 0; i < 3; i++){
  172. arr[i]->eat();
  173. }
  174.  
  175. // shouldn't wag tails, they are sleeping
  176. // for (int i = 0; i < 3; i++){
  177. // arr[i]->wagTail();
  178. }
  179.  
  180. // Tail t("Taily", 2);
  181. // t.makeSound();
  182. //}
  183.  
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
Woof!
Meow!
Woof!
The mammal is eating.
The mammal is eating.
The mammal is eating.
The dog is sleeping.
The cat is sleeping.
The dog is sleeping.
Woof!
Meow!
Woof!
The mammal is eating.
The mammal is eating.
The mammal is eating.