Advertisement
irmantas_radavicius

Untitled

Apr 23rd, 2024
773
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <vector>
  5. #include <cmath>
  6. #include <ctime>
  7. #include <cctype>
  8.  
  9. using namespace std;
  10.  
  11.  
  12. class Animal {
  13.     public:
  14.         virtual void makeSound() = 0;
  15. };
  16. class Dog : public Animal {
  17.     public:
  18.         virtual void makeSound(){
  19.             cout << "Woof!" << endl;
  20.         }
  21. };
  22. class Cat : public Animal {
  23.     public:
  24.         virtual void makeSound(){
  25.             cout << "Meaaaaw!" << endl;
  26.         }
  27. };
  28. class Parrot : public Animal {
  29.     public:
  30.         virtual void makeSound(){
  31.             cout << "Hello, I'm a parrot!" << endl;
  32.         }
  33. };
  34.  
  35. int main(){
  36.  
  37.     vector<Animal *> zoo;
  38.  
  39.     srand(time(NULL));
  40.     for(int i = 0; i < 10; i++){
  41.         /*
  42.         Animal *a;
  43.         switch(rand()%3){
  44.             case 0:
  45.                 a = new Dog();
  46.                 break;
  47.             case 1:
  48.                 a = new Cat();
  49.                 break;
  50.             case 2:
  51.                 a = new Parrot();
  52.                 break;
  53.         }
  54.         zoo.push_back(a);
  55.         */
  56.         zoo.push_back(new Parrot());
  57.  
  58.     }
  59.  
  60.     for(int i = 0; i < 10; i++){
  61.         zoo[i]->makeSound();
  62.     }
  63.  
  64.     for(int i = 0; i < 10; i++){
  65.         delete zoo[i];
  66.     }
  67.  
  68.  
  69.     return 0;
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement