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