Advertisement
Pr0nly

k2_3

Jan 21st, 2020
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class robot{
  7. public:
  8.     virtual void praca()const = 0;
  9.     virtual ostream& wyswietl(ostream& out)const = 0;
  10.     virtual ~robot(){}
  11. };
  12.  
  13. class rt1: public robot{
  14. protected:
  15.     string * t;
  16. public:
  17.     rt1(): t(new string("---")){}
  18.     rt1(const string& a): t(new string(a)){}
  19.     rt1(const rt1& r): t(new string(*r.t)){}
  20.  
  21.     rt1& operator=(const rt1& r){
  22.         if(this!=&r){
  23.             delete t;
  24.             t = new string(*r.t);
  25.         }
  26.         return *this;
  27.     }
  28.  
  29.  
  30.     void praca()const {cout << *t << endl;}
  31.     ostream& wyswietl(ostream& out)const{
  32.         return out << *t << endl;;
  33.     }
  34.  
  35.     ~rt1(){delete t;}
  36. };
  37.  
  38. class rt2: public rt1{
  39.     int l;
  40. public:
  41.     rt2(): l(0){}
  42.     rt2(const string &a1, const int& a2): rt1(a1), l(a2){}
  43.    
  44.     void praca()const {cout << *t << ' '<< l << endl;}
  45.     ostream& wyswietl(ostream& out)const{
  46.         return out << *t << ' '<< l << endl;
  47.     }
  48.  
  49.     rt2& operator-=(const int& i){
  50.         if(i != 0){l -= i;}
  51.         return *this;
  52.     }
  53. };
  54.  
  55. robot& operator -= (robot& r, const int& i) {
  56.     *(static_cast<rt2*>(&r))-=i;
  57.     return r;
  58. }
  59.  
  60. ostream& operator<<(ostream& out, const robot& r){
  61.     return r.wyswietl(out);
  62. }
  63.  
  64. int main(){
  65.  
  66.     const rt1 odbior("odebrano gotowy produkt");
  67.     robot * linia[5];
  68.  
  69.     linia[0] = new rt1("polozono");
  70.     linia[1] = new rt2("uderzono", 5);
  71.     linia[2] = new rt2("prawo", 4);
  72.     linia[3] = new rt2("uderzono", 7);
  73.     linia[4] = new rt1(odbior);
  74.    
  75.     for(int i=0; i<5; ++i)
  76.         linia[i] -> praca();
  77.  
  78.     cout << "********* 3 **********" << endl;
  79.  
  80.     *linia[3] -= 5;
  81.  
  82.     for(int i=0; i<5; ++i) {
  83.         cout << *linia[i];
  84.         delete linia[i];
  85.     }
  86.  
  87.     cout << "********* 4 **********" << endl;
  88.  
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement