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

Eating:
The dog is eating.
The cat is eating.
The dog is eating.

Sleeping:
Fido is sleeping.
Fluffy is sleeping.
Barky is sleeping.

Trying to make sounds while sleeping:

Trying to eat while sleeping:

Trying to wag tails:
The dog can't wag its tail because it's sleeping.
The dog can't wag its tail because it's sleeping.

Wake up one dog:
Fido woke up.
The dog is wagging its tail.

Tail object:
Woof!