#include #include namespace CGI { enum GrafickyTyp { nuhelnik = 0, bod, trojuhelnik, cara, obdelnik }; } class KresliciObjekt { public: KresliciObjekt () { } ~KresliciObjekt () { } CGI::GrafickyTyp typ () const = 0; virtual void kresli (int platno) const = 0; }; class Bod: public KresliciObjekt { private: int _x, _y; public: Bod (int x, int y) :KresliciObjekt(), _x(x), _y(y) { } const int& x () const { return _x; } const int& y () const { return _y; } void kresli (int platno) { std::cout << "kreslim bod [" << _x << ", " << _y << "] na platno " << platno << std::endl; } CGI::GrafickyTyp typ () const { return CGI::bod; } void nastav (int x, int y) { _x = x; _y = y; } }; class NUhelnik: private KresliciObjekt { protected: int _pocetVrcholu; Bod *_vrcholy; public: NUhelnik () :KresliciObjekt(), _pocetVrcholu(0), _vrcholy(0) { } NUhelnik (int pocetVrcholu, Bod* vrcholy) :KresliciObjekt() { nastavVrcholy(pocetVrcholu, vrcholy); } NUhelnik (const NUhelnik &n) :KresliciObjekt(), _vrcholy(0) { nastavVrcholy(n._pocetVrcholu, n._vrcholy); } NUhelnik& operator = (const NUhelnik &n) { nastavVrcholy(n._pocetVrcholu, n._vrcholy); return *this; } ~NUhelnik () { delete _vrcholy; } GrafickyTyp typ () const { return CGI::nuhelnik; } void kresli (int platno) const { std::cout << "kreslim nuhelnik s " << _pocetVrcholu << " vrcholy na platno " << platno << std::endl; for (int i = 0; i < _pocetVrcholu; i++) { _vrcholy[i].kresli(platno); } } void nastavVrcholy (int pocetVrcholu, Bod *vrcholy) { _vrcholy = new Bod[pocetVrcholu]; memcpy(_vrcholy, vrcholy, pocetVrcholu*sizeof(Bod)); _pocetVrcholu = pocetVrcholu; } } class Trojuhelnik: public NUhelnik { public: Trojuhelnik () :NUhelnik() { Bod pole[3]; pole[0].nastav(-1, 0); pole[1].nastav(1, 0); nastavVrcholy(3, pole); } CGI::GrafickyTyp typ () const { return CGI::trojuhelnik; } }; class Cara: public KresliciObjekt { private: int _x1, _y1, _x2, _y2; public: Cara () :_x1(0), _y1(0), _x2(0), _y2(0) { } Cara (int x1, int y1, int x2, int y2) :KresliciObjekt(), _x1(x1), _y1(y1), _x2(x2), _y2(y2) { } Cara (const Bod &b1, const Bod &b2) :KresliciObjekt(), _x1(b1.x), _y1(b1.y()), _x2(b2.x()), _y2(b2.y()) { } void kresli (int platno) const { std::cout << "kreslim caru z [" << _x1 << ", " << _y1 << "] do [" << _x2 << ", " << _y2 << "] na platno " << platno << std::endl; } CGI::GrafickyTyp typ () const { return CGI::cara; } }; class Obdelnik: public NUhelnik { public: Obdelnik () :NUhelnik() { Bod pole[4]; pole[1].nastav(-2, -1); pole[2].nastav(2, -1); pole[3].nastav(2, 1); pole[4].nastav(-2, 1); nastavVrcholy(4, pole); } CGI::GrafickyTyp typ () const { return CGI::obdelnik; } }; const int pocetPrvku = 6; KresliciObjekt** vytvorPrvky () { KresliciObjekt **pole = new KresliciObjekt*[pocetPrvku]; pole[0] = new Obdelnik(); pole[1] = new Cara(-2, 2, 5, 12); pole[2] = new Bod (-1, 1); pole[3] = new Trojuhelnik(); pole[4] = new NUhelnik(); pole[5] = new NUhelnik(); return pole; } void znicPrvky (KresliciObjekt **pole) { for (int i = 0; i < pocetPrvku; i++) { delete pole[i]; } delete pole; } int main (int argc, char **argv) { int platno = 3; KresliciObjekt **pole = vytvorPrvky(); Bod vrcholy[5]; vrcholy[0].nastav(-1, -1); vrcholy[1].nastav(1, -1); vrcholy[2].nastav(1, 1); vrcholy[3].nastav(0, 2); vrcholy[4].nastav(-1, 1); for (int i = 0; i < pocetPrvku; i++) { if (pole[i]->typ() == CGI::nuhelnik) { NUhelnik *u = static_cast(pole[i]); u->nastavVrcholy(5 + pocetPrvku - i - 2, vrcholy - pocetPrvku + i + 2); } } NUhelnik *uh1 = static_cast(pole[pocetPrvku - 1]); NUhelnik *uh2 = static_cast(pole[pocetPrvku - 2]); *uh1 = *uh2; for (int i = 0; i < pocetPrvku; i++) { pole[i]->kresli(platno); } znicPrvky(pole); return 0; }