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