Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string.h>
- using namespace std;
- class Jivotno
- {
- protected :
- char* hrana;
- void copy(const char*);
- public :
- Jivotno(const char* = " ");
- Jivotno(const Jivotno&);
- Jivotno& operator=(const Jivotno&);
- ~Jivotno();
- virtual int getKoef() const = 0;
- virtual Jivotno* clone() const = 0;
- virtual void print() const = 0;
- };
- void Jivotno::copy(const char* hrana)
- {
- this->hrana = new char[strlen(hrana) + 1];
- strcpy(this->hrana,hrana);
- }
- Jivotno::Jivotno(const char* hrana)
- {
- copy(hrana);
- }
- Jivotno::Jivotno(const Jivotno& other)
- {
- copy(other.hrana);
- }
- Jivotno& Jivotno::operator=(const Jivotno& other)
- {
- if (this != &other)
- {
- delete[] hrana;
- copy(other.hrana);
- }
- return *this;
- }
- Jivotno::~Jivotno()
- {
- delete[] hrana;
- }
- class GorskiJivotni : virtual public Jivotno
- {
- protected :
- int visochina;
- public :
- GorskiJivotni(int = 0, const char* = " ");
- int getKoef() const = 0;
- Jivotno* clone() const = 0;
- void print() const = 0;
- };
- GorskiJivotni::GorskiJivotni(int visochina, const char* hrana)
- : Jivotno(hrana)
- {
- this->visochina = visochina;
- }
- class Hishtnik : virtual public Jivotno
- {
- protected :
- int zahapka;
- int skorost;
- public :
- Hishtnik(int = 0, int = 0, const char* = " ");
- int getKoef() const = 0;
- Jivotno* clone() const = 0;
- void print() const = 0;
- };
- Hishtnik::Hishtnik(int zahapka, int skorost, const char* hrana)
- : Jivotno(hrana)
- {
- this->zahapka = zahapka;
- this->skorost = skorost;
- }
- class Trevopasno : virtual public Jivotno
- {
- protected :
- int treva;
- public :
- Trevopasno(int = 0, const char* = " ");
- int getKoef() const = 0;
- Jivotno* clone() const = 0;
- void print() const = 0;
- };
- Trevopasno::Trevopasno(int treva, const char* hrana)
- : Jivotno(hrana)
- {
- this->treva = treva;
- }
- class Mechka : public Hishtnik,public GorskiJivotni
- {
- public :
- Mechka(const char* = " ",int = 0,int = 0,int = 0);
- int getKoef() const;
- Jivotno* clone() const;
- void print() const;
- };
- Mechka::Mechka(const char* hrana,int skorost,int zahapka,int visochina)
- : Jivotno(hrana),
- Hishtnik(zahapka,skorost,hrana),
- GorskiJivotni(visochina,hrana)
- {}
- int Mechka::getKoef() const
- {
- return (zahapka*skorost) / visochina;
- }
- Jivotno* Mechka::clone() const
- {
- return new Mechka(*this);
- }
- void Mechka::print() const
- {
- cout << hrana << " " << skorost << " " << zahapka << " " << visochina << endl;
- }
- class Surna : public GorskiJivotni, public Trevopasno
- {
- public :
- Surna(const char* = " ", int = 0, int = 0);
- int getKoef() const;
- Jivotno* clone() const;
- void print() const;
- };
- Surna::Surna(const char* hrana, int visochina, int treva)
- : Jivotno(hrana), GorskiJivotni(visochina, hrana), Trevopasno(treva, hrana)
- {}
- int Surna::getKoef() const
- {
- return (treva / visochina);
- }
- Jivotno* Surna::clone() const
- {
- return new Surna(*this);
- }
- void Surna::print() const
- {
- cout << hrana << " " << visochina << " " << treva << endl;
- }
- class Reservat
- {
- private :
- Jivotno** jivotni;
- int cap;
- int size;
- void resize();
- void remove();
- void copy(const Reservat&);
- public :
- Reservat();
- Reservat(const Reservat&);
- Reservat& operator=(const Reservat&);
- ~Reservat();
- void addObject(const Jivotno&);
- void print() const;
- void sort();
- };
- void Reservat::copy(const Reservat& other)
- {
- this->cap = other.cap;
- this->size = other.size;
- this->jivotni = new Jivotno*[other.size];
- for(int i = 0;i < other.cap;i++)
- {
- this->jivotni[i] = other.jivotni[i]->clone();
- }
- }
- void Reservat::remove()
- {
- for(int i = 0;i < size;i++)
- {
- delete[] jivotni[i];
- }
- delete[] jivotni;
- }
- void Reservat::resize()
- {
- Jivotno** temp = new Jivotno*[2 * cap];
- for(int i = 0;i < size;i++)
- {
- temp[i] = jivotni[i];
- }
- cap *= 2;
- delete[] jivotni;
- jivotni = temp;
- }
- Reservat::Reservat() : cap(2),size(0)
- {
- jivotni = new Jivotno*[cap];
- }
- Reservat::Reservat(const Reservat& other)
- {
- copy(other);
- }
- Reservat& Reservat::operator=(const Reservat& other)
- {
- if(this != &other)
- {
- remove();
- copy(other);
- }
- return *this;
- }
- Reservat::~Reservat()
- {
- remove();
- }
- void Reservat::addObject(const Jivotno& animal)
- {
- if(size == cap)
- {
- resize();
- }
- jivotni[size++] = animal.clone();
- }
- void Reservat::print() const
- {
- for(int i = 0;i < size;i++)
- {
- jivotni[i]->print();
- }
- }
- void Reservat::sort()
- {
- for(int i = 0;i < size - 1;i++)
- {
- for(int j = 0;j < size - 1;j++)
- {
- if(jivotni[j]->getKoef() > jivotni[j + 1]->getKoef())
- {
- Jivotno* temp = jivotni[j];
- jivotni[j] = jivotni[j + 1];
- jivotni[j + 1] = temp;
- }
- }
- }
- print();
- }
- int main()
- {
- Reservat a;
- a.addObject(Mechka("riba",150,100,20));
- a.addObject(Mechka("zaici",300,100,20));
- a.addObject(Mechka("rasteniq",150,120,20));
- a.addObject(Mechka("riba",100,400,20));
- a.addObject(Surna("juludi",100,200));
- a.print();
- a.sort();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement