Advertisement
Guest User

figura zle xD

a guest
Jan 21st, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class figura {
  7. public:
  8.     virtual double pole()const = 0;
  9.     virtual ostream& wyswietl(ostream& out)const = 0;
  10.     virtual ~figura() {}
  11. };
  12.  
  13. class kolo : public figura {
  14.     string* txt;
  15.     double l;
  16. public:
  17.     kolo() :txt(new string("brak")), l(0) {}
  18.     kolo(const string& a1, const double& a2) :txt(new string(a1)), l(a2) {}
  19.     kolo(const kolo& r) :txt(new string(*r.txt)), l(r.l) {}
  20.  
  21.     kolo& operator=(const kolo& r) {
  22.         if (this != &r) {
  23.             delete txt;
  24.             txt = new string(*r.txt);
  25.             l = r.l;
  26.         }return *this;
  27.     }
  28.  
  29.     double pole() const { return 3.14 * l * l; }
  30.  
  31.     ostream& wyswietl(ostream& out)const { return out << *txt << " " << l; }
  32.  
  33.     ~kolo() { delete txt; }
  34. };
  35.  
  36. class prost : public figura {
  37. protected:
  38.     string* txt;
  39.     double a;
  40.     double b;
  41. public:
  42.     prost() :txt(new string("brak")), a(0), b(0) {}
  43.     prost(const string& a1, const double& a2, const double& a3) :txt(new string(a1)), a(a2), b(a3) {}
  44.     prost(const prost& r) :txt(new string(*r.txt)), a(r.a), b(r.b) {}
  45.  
  46.     prost& operator=(const prost& r) {
  47.         if (this != &r) {
  48.             delete txt;
  49.             txt = new string(*r.txt);
  50.             a = r.a;
  51.             b = r.b;
  52.         }return *this;
  53.     }
  54.  
  55.     double pole()const { return a * b; }
  56.  
  57.  
  58.  
  59.     ostream& wyswietl(ostream& out)const { return out << *txt << " " << a << " " << b; }
  60.  
  61.     ~prost() { delete txt; }
  62. };
  63.  
  64. class prostop : public prost {
  65.     double h;
  66. public:
  67.     prostop() :prost(), h(0) {}
  68.     prostop(const string& a1, const double& a2, const double& a3, const double& a4) :prost(a1, a2, a3), h(a4) {}
  69.     prostop(const prostop& r) :prost(r), h(r.h) {}
  70.  
  71.     double pole()const { return 2 * a * b + 2 * a * h + 2 * b * h; }
  72.  
  73.  
  74.  
  75.     ostream& wyswietl(ostream& out)const { return out << *txt << " " << a << " " << b << " " << h; }
  76. };
  77.  
  78.  
  79.  
  80. ostream& operator<<(ostream& out, const figura& r) {
  81.     return r.wyswietl(out) << endl;
  82. }
  83. int main()
  84. {
  85.     figura* tab[5];
  86.     const kolo test1("czarny", 100);
  87.     const prost test2("szary", 2.2, 2);
  88.  
  89.     tab[0] = new kolo("czerwony", 100);
  90.     tab[1] = new kolo;
  91.     tab[2] = new prost("niebieski", 1, 1);
  92.     tab[3] = new prostop("zielony", 1, 1, 1);
  93.     tab[4] = new prostop;
  94.  
  95.     for (int i = 0; i < 5; ++i) {
  96.         cout << tab[i]->pole() << endl;
  97.     }
  98.     cout << "************* 3 ************" << endl;
  99.  
  100.     for (int i = 0; i < 5; ++i) {
  101.         cout << *tab[i] << tab[i]->pole() << endl;
  102.         delete tab[i];
  103.     }
  104.  
  105.     cout << "************* 4 ************" << endl;
  106.  
  107.  
  108.  
  109.  
  110.     cout << "************* 5 ************" << endl;
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement