Advertisement
Guest User

robocik

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