Pr0nly

g2_pp

Jan 14th, 2020
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class robot {
  7. public:
  8.     static int liczba_obiektow;
  9.     robot() { liczba_obiektow++; }
  10.     virtual ~robot() { liczba_obiektow--; }
  11.     virtual void praca() const = 0;
  12.     virtual ostream& wyswietl(ostream& out) const = 0;
  13. };
  14. int robot::liczba_obiektow = 0;
  15.  
  16. class rt1: public robot {
  17. protected:
  18.     string *txt;
  19.  
  20. public:
  21.     rt1(): txt(new string("---")) {}
  22.     rt1(const string& a): txt(new string(a)) {}
  23.     rt1(const rt1& a): txt(new string(*a.txt)) {}
  24.  
  25.     rt1& operator = (const rt1& a) {
  26.         if(this != &a) {
  27.             delete txt;
  28.             txt = new string(*a.txt);
  29.         }
  30.         return *this;
  31.     }
  32.  
  33.     ~rt1() { delete txt; }
  34.  
  35.     ostream& wyswietl(ostream& out) const  {
  36.         return out << *txt << endl;
  37.     }
  38.     void praca() const { wyswietl(cout); }
  39.  
  40. };
  41.  
  42. class rt2: public rt1 {
  43. protected:
  44.     int ilosc;
  45.  
  46. public:
  47.     rt2(): rt1(), ilosc() {}
  48.     rt2(const string& a, const int& b): rt1(a), ilosc(b) {}
  49.  
  50.     ostream& wyswietl(ostream& out) const  {
  51.         if(*txt == "---" && ilosc == 0) throw string("robot niezaprogramowany");
  52.         return out << *txt << " " << ilosc << endl;
  53.     }
  54.     void praca() const { wyswietl(cout); }
  55.  
  56.  
  57.     rt2& operator -= (const int& i) {
  58.         ilosc -= i;
  59.         return *this;
  60.     }
  61. };
  62.  
  63. robot& operator -= (robot& r, const int& i) {
  64.     *(static_cast<rt2*>(&r))-=i;
  65.     return r;
  66. }
  67.  
  68. ostream& operator << (ostream& out, const robot& r) {
  69.     return r.wyswietl(out);
  70. }
  71.  
  72.  
  73. int main() {
  74.     const rt1 odbior("odebrano gotowy produkt");
  75.  
  76.     robot *linia [5];
  77.     linia[0] = new rt1("polozono");
  78.     linia[1] = new rt2("uderzono", 5);
  79.     linia[2] = new rt2("prawo", 4);
  80.     linia[3] = new rt2("uderzono", 7);
  81.     linia[4] = new rt1(odbior);
  82.  
  83.     for(int i=0; i<5; ++i)
  84.         linia[i] -> praca();
  85.  
  86.     cout << "********* 3 **********" << endl;
  87.  
  88.     *linia[3] -= 5;
  89.  
  90.     for(int i=0; i<5; ++i) {
  91.         cout << *linia[i];
  92.         delete linia[i];
  93.     }
  94.  
  95.     cout << "********* 4 **********" << endl;
  96.  
  97.     cout << "Liczba robotow: " << robot::liczba_obiektow << endl;
  98.     linia[0] = new rt1("polozono");
  99.     linia[1] = new rt2;
  100.     linia[2] = new rt1("polozono");
  101.     linia[3] = new rt2;
  102.     linia[4] = new rt1(odbior);
  103.     cout << "Liczba robotow: " << robot::liczba_obiektow << endl;
  104.  
  105.  
  106.     for(int i=0; i<5; ++i)
  107.         try {
  108.             cout << *linia[i];
  109.         }
  110.         catch(const string& e) { cout << e << endl; }
  111.     cout << "********* 5 **********" << endl;
  112.     for(int i=0; i<5; ++i)
  113.         delete linia[i];
  114.  
  115.  
  116.     return 0;
  117. }
Add Comment
Please, Sign In to add comment