CPP   12

Zivotinja

Guest on 5th July 2022 04:58:31 PM

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class Zivotinja {
  6. public:
  7.   Zivotinja(double tezina = 0)
  8.     :_tezina(tezina)
  9.   {
  10.     cout << "Konstruktor zivotinje" << endl;
  11.   }
  12.   double vratiTezinu() const {
  13.     return _tezina;
  14.   }
  15.   virtual void kaziZdravo() const = 0;
  16.   virtual ~Zivotinja() {
  17.     cout << "Destruktor zivotinje" << endl;
  18.   }
  19. private:
  20.   double _tezina;
  21. };
  22.  
  23. class Pas : public Zivotinja {
  24. public:
  25.   Pas(string ime, double tezina)
  26.     :Zivotinja(tezina), _ime(ime)
  27.   {
  28.     cout << "Konstruktor psa" << endl;
  29.   }
  30.   string vratiIme() const {
  31.     return _ime;
  32.   }
  33.   void kaziZdravo() const {
  34.     cout << "Av-av!" << endl;
  35.   }
  36.   ~Pas() {
  37.     cout << "Destruktor psa" << endl;
  38.   }
  39. private:
  40.   string _ime;
  41. };
  42.  
  43. class Macka : public Zivotinja {
  44. public:
  45.   Macka(string ime, double tezina)
  46.     :Zivotinja(tezina), _ime(ime)
  47.   {
  48.     cout << "Konstruktor macke" << endl;
  49.   }
  50.   string vratiIme() const {
  51.     return _ime;
  52.   }
  53.   void kaziZdravo() const {
  54.     cout << "Mjau-mjau!" << endl;
  55.   }
  56.   ~Macka() {
  57.     cout << "Destruktor macke" << endl;
  58.   }
  59. private:
  60.   string _ime;
  61. };
  62.  
  63.  
  64.  
  65. int main() {
  66.   //Zivotinja z(10);
  67.   //cout << "z.vratiTezinu() = " << z.vratiTezinu() << endl;
  68.   //z.kaziZdravo();
  69.  
  70.   Pas p("Zucko", 5);
  71.   cout << "p.vratiTezinu() = " << p.vratiTezinu() << endl;
  72.   cout << "p.vratiIme() = " << p.vratiIme() << endl;
  73.   p.kaziZdravo();
  74.  
  75.   Macka m("Maca", 2);
  76.   cout << "m.vratiTezinu() = " << m.vratiTezinu() << endl;
  77.   cout << "m.vratiIme() = " << m.vratiIme() << endl;
  78.   m.kaziZdravo();
  79.  
  80.   Zivotinja* niz[3];
  81.   niz[0] = new Pas("Micko", 7);
  82.   niz[1] = new Macka("Mica", 3);
  83.   niz[2] = new Pas("Dzeki", 8);
  84.  
  85.   for (unsigned i = 0; i < 3; i++) {
  86.     cout << niz[i]->vratiTezinu() << endl;
  87.     //cout << niz[i]->vratiIme() << endl;
  88.     niz[i]->kaziZdravo();
  89.   }
  90.  
  91.   for (unsigned i = 0; i < 3; i++)
  92.     delete niz[i];
  93.  
  94.   return 0;
  95. }

Raw Paste


Login or Register to edit or fork this paste. It's free.