Advertisement
Guest User

k_figury

a guest
Jan 21st, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. class figura {
  8.     string * kolorFigury;
  9.  
  10. public:
  11.     figura(): kolorFigury(new string("Brak")) {}
  12.  
  13.     figura(const string & kolor) : kolorFigury(new string(kolor)) {}
  14.  
  15.     figura(const figura & obj) : kolorFigury(new string(*obj.kolorFigury)) {}
  16.  
  17.     figura & operator=(const figura & obj) {
  18.         if (&obj != this) {
  19.             delete kolorFigury;
  20.  
  21.             kolorFigury = new string(*obj.kolorFigury);
  22.         }
  23.         return *this;
  24.     }
  25.  
  26.     virtual ~figura() { delete kolorFigury; }
  27.  
  28.     virtual double pole() const = 0;
  29.  
  30.     virtual void wypisz(ostream & stream) const { stream << "Kolor figury: " << *kolorFigury << endl; }
  31.  
  32.     virtual figura & operator*=(const int & val) = 0;
  33.  
  34.     string & kolor() const { return *kolorFigury; }
  35. };
  36.  
  37. class kolo : public figura {
  38.     static unsigned liczba_kol;
  39.     double promien;
  40. public:
  41.     kolo() : promien(0) { liczba_kol++; }
  42.  
  43.     kolo(const string & kolor, const double & promienKola) : figura(kolor), promien(promienKola) { liczba_kol++; }
  44.  
  45.     ~kolo() { liczba_kol--; }
  46.  
  47.     double pole() const { return 3.14 * promien * promien; }
  48.  
  49.     void wypisz(ostream & stream) const { figura::wypisz(stream); stream << "Promien kola: " << promien << endl; }
  50.  
  51.     figura & operator*=(const int & val) { promien *= val; return *this; }
  52. };
  53.  
  54. class prost : public figura {
  55.     static unsigned liczba_prostokat;
  56. protected: double a, b;
  57. public:
  58.     prost() : a(0), b(0) { liczba_prostokat++; }
  59.  
  60.     prost(const string & kolor, const double & a1, const double & b1) : figura(kolor), a(a1), b(b1) { liczba_prostokat++; }
  61.  
  62.     ~prost() { liczba_prostokat--; }
  63.  
  64.     double pole() const { return a * b; }
  65.  
  66.     void wypisz(ostream & stream) const { figura::wypisz(stream); stream << "Wysokosc prostokata: " << a << endl << "Szerokosc prostokata: " << b << endl; }
  67.  
  68.     figura & operator*=(const int & val) { a *= val; b *= val; return *this; }
  69.  
  70.     static const unsigned & liczba_obiektow() { return liczba_prostokat; }
  71. };
  72.  
  73. class prostop : public prost {
  74.     static unsigned liczba_prostopadloscian;
  75.     double h;
  76. public:
  77.     prostop() : h(0) { liczba_prostopadloscian++; }
  78.  
  79.     prostop(const string & kolor, const double & a1, const double & b1, const double & c1) : prost(kolor, a1, b1), h(c1) { liczba_prostopadloscian++; }
  80.  
  81.     ~prostop() { liczba_prostopadloscian--; }
  82.  
  83.     double pole() const { return (2 * (a * b)) + ( 2 * (a * h)) + ( 2 * (b * h)); }
  84.  
  85.     const unsigned & liczba_obiektow() const { return liczba_prostopadloscian; }
  86.  
  87.     void wypisz(ostream & stream) const { figura::wypisz(stream); stream << "Wysokosc prostopadloscianu: " << h << endl << "Szerokosc prostopadloscianu: " << a << endl << "Dlugosc prostopadloscianu: " << b << endl; }
  88.  
  89.     figura & operator*=(const int & val) { prost::operator*=(val); h *= val; return *this; }
  90. };
  91.  
  92. ostream & operator<<(ostream & stream, const figura & obj) {
  93.     obj.wypisz(stream);
  94.  
  95.     return stream;
  96. }
  97.  
  98. figura & operator+(const string & str, figura & obj) {
  99.     obj.kolor().insert(0, str);
  100.  
  101.     return obj;
  102. }
  103.  
  104. figura & operator+(figura & obj, const string & str) {
  105.     obj.kolor().append(str);
  106.  
  107.     return obj;
  108. }
  109.  
  110. unsigned kolo::liczba_kol = 0;
  111. unsigned prost::liczba_prostokat = 0;
  112. unsigned prostop::liczba_prostopadloscian = 0;
  113.  
  114. int main()
  115. {
  116.     figura * tab[5] = { 0, 0, 0, 0, 0 };
  117.  
  118.     const kolo test1("czarny", 100);
  119.     const prostop test2("szary", 2, 2, 2);
  120.  
  121.     tab[0] = new kolo("czerwony", 1);
  122.     tab[1] = new kolo;
  123.     tab[2] = new prost("niebieski", 1, 1);
  124.     tab[3] = new prostop("zielony", 1, 1, 1);
  125.     tab[4] = new prostop;
  126.  
  127.     for (int i = 0; i < 5; ++i)
  128.         cout << tab[i]->pole() << endl;
  129.  
  130.     cout << "************** 3 **************";
  131.  
  132.     cout << endl;
  133.  
  134.     for (int i = 0; i < 5; ++i)
  135.         cout << *tab[i] << tab[i]->pole() << endl << endl;
  136.  
  137.     cout << "************** 4 **************";
  138.  
  139.     cout << endl;
  140.  
  141.  
  142.     *tab[1] = test1;
  143.     *tab[4] = test2;
  144.     *tab[2] = "jasno " + *tab[2];
  145.     *tab[4] *= 2;
  146.  
  147.     for (int i = 0; i < 5; ++i)
  148.         cout << *tab[i] << tab[i]->pole() << endl << endl;
  149.  
  150.     cout << "$ " << prost::liczba_obiektow() << endl;
  151.  
  152.     for (int i = 0; i < 5; ++i)
  153.         delete tab[i];
  154.  
  155.     cout << "$ " << prost::liczba_obiektow() << endl;
  156.  
  157.     return 0;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement