Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- class figura {
- string * kolorFigury;
- public:
- figura(): kolorFigury(new string("Brak")) {}
- figura(const string & kolor) : kolorFigury(new string(kolor)) {}
- figura(const figura & obj) : kolorFigury(new string(*obj.kolorFigury)) {}
- figura & operator=(const figura & obj) {
- if (&obj != this) {
- delete kolorFigury;
- kolorFigury = new string(*obj.kolorFigury);
- }
- return *this;
- }
- virtual ~figura() { delete kolorFigury; }
- virtual double pole() const = 0;
- virtual void wypisz(ostream & stream) const { stream << "Kolor figury: " << *kolorFigury << endl; }
- virtual figura & operator*=(const int & val) = 0;
- string & kolor() const { return *kolorFigury; }
- };
- class kolo : public figura {
- static unsigned liczba_kol;
- double promien;
- public:
- kolo() : promien(0) { liczba_kol++; }
- kolo(const string & kolor, const double & promienKola) : figura(kolor), promien(promienKola) { liczba_kol++; }
- ~kolo() { liczba_kol--; }
- double pole() const { return 3.14 * promien * promien; }
- void wypisz(ostream & stream) const { figura::wypisz(stream); stream << "Promien kola: " << promien << endl; }
- figura & operator*=(const int & val) { promien *= val; return *this; }
- };
- class prost : public figura {
- static unsigned liczba_prostokat;
- protected: double a, b;
- public:
- prost() : a(0), b(0) { liczba_prostokat++; }
- prost(const string & kolor, const double & a1, const double & b1) : figura(kolor), a(a1), b(b1) { liczba_prostokat++; }
- ~prost() { liczba_prostokat--; }
- double pole() const { return a * b; }
- void wypisz(ostream & stream) const { figura::wypisz(stream); stream << "Wysokosc prostokata: " << a << endl << "Szerokosc prostokata: " << b << endl; }
- figura & operator*=(const int & val) { a *= val; b *= val; return *this; }
- static const unsigned & liczba_obiektow() { return liczba_prostokat; }
- };
- class prostop : public prost {
- static unsigned liczba_prostopadloscian;
- double h;
- public:
- prostop() : h(0) { liczba_prostopadloscian++; }
- prostop(const string & kolor, const double & a1, const double & b1, const double & c1) : prost(kolor, a1, b1), h(c1) { liczba_prostopadloscian++; }
- ~prostop() { liczba_prostopadloscian--; }
- double pole() const { return (2 * (a * b)) + ( 2 * (a * h)) + ( 2 * (b * h)); }
- const unsigned & liczba_obiektow() const { return liczba_prostopadloscian; }
- void wypisz(ostream & stream) const { figura::wypisz(stream); stream << "Wysokosc prostopadloscianu: " << h << endl << "Szerokosc prostopadloscianu: " << a << endl << "Dlugosc prostopadloscianu: " << b << endl; }
- figura & operator*=(const int & val) { prost::operator*=(val); h *= val; return *this; }
- };
- ostream & operator<<(ostream & stream, const figura & obj) {
- obj.wypisz(stream);
- return stream;
- }
- figura & operator+(const string & str, figura & obj) {
- obj.kolor().insert(0, str);
- return obj;
- }
- figura & operator+(figura & obj, const string & str) {
- obj.kolor().append(str);
- return obj;
- }
- unsigned kolo::liczba_kol = 0;
- unsigned prost::liczba_prostokat = 0;
- unsigned prostop::liczba_prostopadloscian = 0;
- int main()
- {
- figura * tab[5] = { 0, 0, 0, 0, 0 };
- const kolo test1("czarny", 100);
- const prostop test2("szary", 2, 2, 2);
- tab[0] = new kolo("czerwony", 1);
- tab[1] = new kolo;
- tab[2] = new prost("niebieski", 1, 1);
- tab[3] = new prostop("zielony", 1, 1, 1);
- tab[4] = new prostop;
- for (int i = 0; i < 5; ++i)
- cout << tab[i]->pole() << endl;
- cout << "************** 3 **************";
- cout << endl;
- for (int i = 0; i < 5; ++i)
- cout << *tab[i] << tab[i]->pole() << endl << endl;
- cout << "************** 4 **************";
- cout << endl;
- *tab[1] = test1;
- *tab[4] = test2;
- *tab[2] = "jasno " + *tab[2];
- *tab[4] *= 2;
- for (int i = 0; i < 5; ++i)
- cout << *tab[i] << tab[i]->pole() << endl << endl;
- cout << "$ " << prost::liczba_obiektow() << endl;
- for (int i = 0; i < 5; ++i)
- delete tab[i];
- cout << "$ " << prost::liczba_obiektow() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement