Advertisement
Guest User

k_roboty

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