Advertisement
koyukix

lab5

Jan 11th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4. using namespace std;
  5.  
  6. class zwierze
  7. {
  8. public:
  9. zwierze() { cout << "Konstruktor zwierze" << endl; }
  10. virtual~zwierze() { cout << "Destruktor zwierze" << endl; }
  11. virtual void opis() { cout << "Metoda zwierze" << endl; }
  12. };
  13. class ssak: public zwierze
  14. {
  15. public:
  16. ssak () { cout << "Konstruktor ssak" << endl; }
  17. virtual ~ssak() { cout << "Destruktor ssak" << endl; }
  18. virtual void opis() { cout << "Metoda ssak" << endl; }
  19. };
  20. class czlowiek: public ssak
  21. {
  22. public:
  23. czlowiek() { cout << "Konstruktor czlowiek" << endl; }
  24. ~czlowiek() { cout << "Destruktor czlowiek" << endl; }
  25. void opis() { cout << "Metoda czlowiek" << endl; }
  26. };
  27. class ptak: public zwierze
  28. {
  29. public:
  30. ptak() { cout << "Konstruktor ptak" << endl; }
  31. ~ptak() { cout << "Destruktor ptak" << endl; }
  32. void opis() { cout << "Metoda ptak" << endl; }
  33. };
  34.  
  35. zwierze* funkcja()
  36. {
  37. srand(time(NULL));
  38. int wartosc=rand() % 5;
  39.  
  40. switch (wartosc)
  41. {
  42. case (0): {return new zwierze;break;}
  43.  
  44. case (1): {return new ssak;break;}
  45.  
  46. case (2): {return new czlowiek;break;}
  47.  
  48. case (3): {return new ptak;break;}
  49.  
  50. case (4): {NULL;}
  51. }
  52. };
  53.  
  54.  
  55. int main()
  56. {
  57. char znak;
  58. do
  59. {
  60. zwierze* wsk=funkcja();
  61.  
  62. if (wsk!=NULL)
  63. wsk->opis();
  64.  
  65. else
  66. cout<<"Pusty los"<<endl;
  67.  
  68. delete wsk;
  69. cin>>znak;
  70.  
  71. } while (znak!='q');
  72.  
  73. return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement