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