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