Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- class robot{
- public:
- virtual void praca()const = 0;
- virtual ostream& wyswietl(ostream& out)const = 0;
- virtual ~robot(){}
- };
- class rt1: public robot{
- protected:
- string * t;
- public:
- rt1(): t(new string("---")){}
- rt1(const string& a): t(new string(a)){}
- rt1(const rt1& r): t(new string(*r.t)){}
- rt1& operator=(const rt1& r){
- if(this!=&r){
- delete t;
- t = new string(*r.t);
- }
- return *this;
- }
- void praca()const {cout << *t << endl;}
- ostream& wyswietl(ostream& out)const{
- return out << *t << endl;;
- }
- ~rt1(){delete t;}
- };
- class rt2: public rt1{
- int l;
- public:
- rt2(): l(0){}
- rt2(const string &a1, const int& a2): rt1(a1), l(a2){}
- void praca()const {cout << *t << ' '<< l << endl;}
- ostream& wyswietl(ostream& out)const{
- return out << *t << ' '<< l << endl;
- }
- rt2& operator-=(const int& i){
- if(i != 0){l -= i;}
- return *this;
- }
- };
- robot& operator -= (robot& r, const int& i) {
- *(static_cast<rt2*>(&r))-=i;
- return r;
- }
- ostream& operator<<(ostream& out, const robot& r){
- return r.wyswietl(out);
- }
- int main(){
- const rt1 odbior("odebrano gotowy produkt");
- robot * linia[5];
- linia[0] = new rt1("polozono");
- linia[1] = new rt2("uderzono", 5);
- linia[2] = new rt2("prawo", 4);
- linia[3] = new rt2("uderzono", 7);
- linia[4] = new rt1(odbior);
- for(int i=0; i<5; ++i)
- linia[i] -> praca();
- cout << "********* 3 **********" << endl;
- *linia[3] -= 5;
- for(int i=0; i<5; ++i) {
- cout << *linia[i];
- delete linia[i];
- }
- cout << "********* 4 **********" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement