nelson33

Untitled

May 9th, 2023
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.39 KB | Source Code | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Zoo {
  6.     public:
  7.     int animalCount;
  8.     Zoo() { animalCount = 0; }
  9.     void born(string name) {
  10.         animalCount += 1;
  11.         cout << "New " << name << " Born!\n";
  12.         cnt();
  13.     }
  14.     void dead(string name) {
  15.         animalCount -= 1;
  16.         cout << "One " + name + " Dead!\n";
  17.         cnt();
  18.     }
  19.     void cnt() { cout << "Now Zoo have " << (animalCount) << " animals!\n"; }
  20. };
  21. class Animal {
  22.     public:
  23.     Zoo *belong;
  24.     string species;
  25.     int legs;
  26.     Animal(Zoo *zoo, string name); // TODO
  27.     virtual void barking() = 0;
  28.     virtual void throwBall() = 0;
  29.     virtual void carton() = 0;
  30.     void printSpecies() { cout << this->species << "\n"; }
  31.     virtual ~Animal() {
  32.         if (this->belong != NULL)
  33.         this->belong->dead(this->species);
  34.     }; // Please Ensure that you know what this destructure is.
  35. };
  36. class Dog : public virtual Animal {
  37.     public:
  38.     Dog(Zoo *zoo); // TODO
  39.     void barking() { cout << "woof!\n"; }
  40.     void throwBall() { cout << "it looks happy!\n"; }
  41.     virtual void carton(){};
  42.     ~Dog(); // TODO
  43. };
  44. class Cat : public virtual Animal {
  45.     public:
  46.     Cat(Zoo *zoo); // TODO
  47.     void barking() { cout << "meow!\n"; }
  48.     void carton() { cout << "it looks so happy!\n"; }
  49.     virtual void throwBall(){};
  50.     ~Cat(); // TODO
  51. };
  52. class Caog : public Dog, public Cat {
  53.     public:
  54.     Caog(Zoo *zoo);         // TODO
  55.     virtual void barking(); // TODO
  56.     void carton();          // TODO
  57.     void throwBall();       // TODO
  58.     ~Caog();                // TODO
  59. };
  60.  
  61. Animal::Animal(Zoo *zoo, string name){
  62.     zoo->born(name);
  63.     belong = zoo;
  64.     species = name;
  65. }
  66.  
  67. Dog::Dog(Zoo *zoo) : Animal(zoo, "Dog"){}
  68.  
  69. Dog::~Dog(){}
  70.  
  71. Cat::Cat(Zoo *zoo) : Animal(zoo, "Cat"){}
  72.  
  73. Cat::~Cat(){}
  74.  
  75. Caog::Caog(Zoo *zoo) : Animal(zoo, "Caog"), Dog(zoo), Cat(zoo){}
  76.  
  77. void Caog::barking(){
  78.     cout << "woof!woof!meow!" << endl;
  79. }
  80.  
  81. void Caog::carton(){
  82.     cout << "it looks so happy!" << endl;
  83. }
  84.  
  85. void Caog::throwBall(){
  86.     cout << "it looks happy!" << endl;
  87. }
  88.  
  89. Caog::~Caog(){}
  90.  
  91. int main() {
  92.     ios_base::sync_with_stdio(false);
  93.     Zoo z;
  94.     int N;
  95.     cin >> N;
  96.     Animal *arr[12];
  97.     int aniTypes;
  98.     for (int i = 0; i < N; i++) {
  99.         cin >> aniTypes;
  100.         if (aniTypes == 0) {
  101.         arr[i] = new Cat(&z);
  102.         } else if (aniTypes == 1) {
  103.         arr[i] = new Dog(&z);
  104.         } else if (aniTypes == 2) {
  105.         arr[i] = new Caog(&z);
  106.         }
  107.     }
  108.     Animal *Ref;
  109.     int T;
  110.     cin >> T;
  111.     int idx, inst;
  112.     for (int i = 0; i < T; i++) {
  113.         cin >> idx >> inst;
  114.         Ref = arr[idx];
  115.         Ref->printSpecies();
  116.         if (inst == 0) {
  117.         Ref->barking();
  118.         } else if (inst == 1) {
  119.         Ref->throwBall();
  120.         } else if (inst == 2) {
  121.         Ref->carton();
  122.         }
  123.     }
  124.     for (int i = 0; i < N; i++) {
  125.         delete arr[i];
  126.     }
  127. }
  128.  
  129. /*3
  130. 0 1 2
  131. 9
  132. 0 0
  133. 0 1
  134. 0 2
  135. 1 0
  136. 1 1
  137. 1 2
  138. 2 0
  139. 2 1
  140. 2 2
  141.  
  142. New Cat Born!
  143. Now Zoo have 1 animals!
  144. New Dog Born!
  145. Now Zoo have 2 animals!
  146. New Caog Born!
  147. Now Zoo have 3 animals!
  148. Cat
  149. meow!
  150. Cat
  151. Cat
  152. it looks so happy!
  153. Dog
  154. woof!
  155. Dog
  156. it looks happy!
  157. Dog
  158. Caog
  159. woof!woof!meow!
  160. Caog
  161. it looks happy!
  162. Caog
  163. it looks so happy!
  164. One Cat Dead!
  165. Now Zoo have 2 animals!
  166. One Dog Dead!
  167. Now Zoo have 1 animals!
  168. One Caog Dead!
  169. Now Zoo have 0 animals!
  170. */
Advertisement
Add Comment
Please, Sign In to add comment