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