Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. using namespace std;
  4. class Jivotno
  5. {
  6. protected :
  7.     char* hrana;
  8.     void copy(const char*);
  9. public :
  10.     Jivotno(const char* = " ");
  11.     Jivotno(const Jivotno&);
  12.     Jivotno& operator=(const Jivotno&);
  13.     ~Jivotno();
  14.     virtual int getKoef() const = 0;
  15.     virtual Jivotno* clone() const = 0;
  16.     virtual void print() const = 0;
  17. };
  18. void Jivotno::copy(const char* hrana)
  19. {
  20.     this->hrana = new char[strlen(hrana) + 1];
  21.     strcpy(this->hrana,hrana);
  22. }
  23. Jivotno::Jivotno(const char* hrana)
  24. {
  25.     copy(hrana);
  26. }
  27. Jivotno::Jivotno(const Jivotno& other)
  28. {
  29.     copy(other.hrana);
  30. }
  31. Jivotno& Jivotno::operator=(const Jivotno& other)
  32. {
  33.     if (this != &other)
  34.     {
  35.         delete[] hrana;
  36.         copy(other.hrana);
  37.     }
  38.     return *this;
  39. }
  40. Jivotno::~Jivotno()
  41. {
  42.     delete[] hrana;
  43. }
  44. class GorskiJivotni : virtual public Jivotno
  45. {
  46. protected :
  47.     int visochina;
  48. public :
  49.     GorskiJivotni(int = 0, const char* = " ");
  50.     int getKoef() const = 0;
  51.     Jivotno* clone() const = 0;
  52.     void print() const = 0;
  53. };
  54. GorskiJivotni::GorskiJivotni(int visochina, const char* hrana)
  55.     : Jivotno(hrana)
  56. {
  57.     this->visochina = visochina;
  58. }
  59. class Hishtnik : virtual public Jivotno
  60. {
  61. protected :
  62.     int zahapka;
  63.     int skorost;
  64. public :
  65.     Hishtnik(int = 0, int = 0, const char* = " ");
  66.     int getKoef() const = 0;
  67.     Jivotno* clone() const = 0;
  68.     void print() const = 0;
  69. };
  70. Hishtnik::Hishtnik(int zahapka, int skorost, const char* hrana)
  71.     : Jivotno(hrana)
  72. {
  73.     this->zahapka = zahapka;
  74.     this->skorost = skorost;
  75. }
  76. class Trevopasno : virtual public Jivotno
  77. {
  78. protected :
  79.     int treva;
  80. public :
  81.     Trevopasno(int = 0, const char* = " ");
  82.     int getKoef() const = 0;
  83.     Jivotno* clone() const = 0;
  84.     void print() const = 0;
  85. };
  86. Trevopasno::Trevopasno(int treva, const char* hrana)
  87.     : Jivotno(hrana)
  88. {
  89.     this->treva = treva;
  90. }
  91. class Mechka : public Hishtnik,public GorskiJivotni
  92. {
  93. public :
  94.     Mechka(const char* = " ",int = 0,int = 0,int = 0);
  95.     int getKoef() const;
  96.     Jivotno* clone() const;
  97.     void print() const;
  98. };
  99. Mechka::Mechka(const char* hrana,int skorost,int zahapka,int visochina)
  100.     : Jivotno(hrana),
  101.       Hishtnik(zahapka,skorost,hrana),
  102.       GorskiJivotni(visochina,hrana)
  103. {}
  104. int Mechka::getKoef() const
  105. {
  106.     return (zahapka*skorost) / visochina;
  107. }
  108. Jivotno* Mechka::clone() const
  109. {
  110.     return new Mechka(*this);
  111. }
  112. void Mechka::print() const
  113. {
  114.     cout << hrana << " " << skorost << " " << zahapka << " " << visochina << endl;
  115. }
  116. class Surna : public GorskiJivotni, public Trevopasno
  117. {
  118. public :
  119.     Surna(const char* = " ", int = 0, int = 0);
  120.     int getKoef() const;
  121.     Jivotno* clone() const;
  122.     void print() const;
  123. };
  124. Surna::Surna(const char* hrana, int visochina, int treva)
  125.     : Jivotno(hrana), GorskiJivotni(visochina, hrana), Trevopasno(treva, hrana)
  126. {}
  127. int Surna::getKoef() const
  128. {
  129.     return (treva / visochina);
  130. }
  131. Jivotno* Surna::clone() const
  132. {
  133.     return new Surna(*this);
  134. }
  135. void Surna::print() const
  136. {
  137.     cout << hrana << " " << visochina << " " << treva << endl;
  138. }
  139. class Reservat
  140. {
  141. private :
  142.     Jivotno** jivotni;
  143.     int cap;
  144.     int size;
  145.     void resize();
  146.     void remove();
  147.     void copy(const Reservat&);
  148. public :
  149.     Reservat();
  150.     Reservat(const Reservat&);
  151.     Reservat& operator=(const Reservat&);
  152.     ~Reservat();
  153.     void addObject(const Jivotno&);
  154.     void print() const;
  155.     void sort();
  156. };
  157. void Reservat::copy(const Reservat& other)
  158. {
  159.     this->cap = other.cap;
  160.     this->size = other.size;
  161.     this->jivotni = new Jivotno*[other.size];
  162.     for(int i = 0;i < other.cap;i++)
  163.     {
  164.         this->jivotni[i] = other.jivotni[i]->clone();
  165.     }
  166. }
  167. void Reservat::remove()
  168. {
  169.     for(int i = 0;i < size;i++)
  170.     {
  171.         delete[] jivotni[i];
  172.     }
  173.     delete[] jivotni;
  174. }
  175. void Reservat::resize()
  176. {
  177.     Jivotno** temp = new Jivotno*[2 * cap];
  178.     for(int i = 0;i < size;i++)
  179.     {
  180.         temp[i] = jivotni[i];
  181.     }
  182.     cap *= 2;
  183.     delete[] jivotni;
  184.     jivotni = temp;
  185. }
  186. Reservat::Reservat() : cap(2),size(0)
  187. {
  188.         jivotni = new Jivotno*[cap];
  189. }
  190. Reservat::Reservat(const Reservat& other)
  191. {
  192.     copy(other);
  193. }
  194. Reservat& Reservat::operator=(const Reservat& other)
  195. {
  196.     if(this != &other)
  197.     {
  198.         remove();
  199.         copy(other);
  200.     }
  201.     return *this;
  202. }
  203. Reservat::~Reservat()
  204. {
  205.     remove();
  206. }
  207. void Reservat::addObject(const Jivotno& animal)
  208. {
  209.     if(size == cap)
  210.     {
  211.         resize();
  212.     }
  213.     jivotni[size++] = animal.clone();
  214. }
  215. void Reservat::print() const
  216. {
  217.     for(int i = 0;i < size;i++)
  218.     {
  219.         jivotni[i]->print();
  220.     }
  221. }
  222. void Reservat::sort()
  223. {
  224.     for(int i = 0;i < size - 1;i++)
  225.     {
  226.         for(int j = 0;j < size - 1;j++)
  227.         {
  228.             if(jivotni[j]->getKoef() > jivotni[j + 1]->getKoef())
  229.             {
  230.                 Jivotno* temp = jivotni[j];
  231.                 jivotni[j] = jivotni[j + 1];
  232.                 jivotni[j + 1] = temp;
  233.             }
  234.         }
  235.     }
  236.     print();
  237. }
  238. int main()
  239. {
  240.     Reservat a;
  241.     a.addObject(Mechka("riba",150,100,20));
  242.     a.addObject(Mechka("zaici",300,100,20));
  243.     a.addObject(Mechka("rasteniq",150,120,20));
  244.     a.addObject(Mechka("riba",100,400,20));
  245.     a.addObject(Surna("juludi",100,200));
  246.     a.print();
  247.     a.sort();
  248.     return 0;
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement