Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- class robot{
- public:
- static int liczba_obiektow;
- robot() {liczba_obiektow++;}
- virtual void praca()const = 0;
- virtual ostream& wyswietl(ostream& out)const = 0;
- virtual robot& operator-=(const int& i){return *this;};
- virtual ~robot(){liczba_obiektow--;}
- };
- int robot::liczba_obiektow = 0;
- 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{
- if(*t == "---" && l == 0) throw string("robot niezaprogramowany");
- return out << *t << ' '<< l << endl;
- }
- robot& operator-=(const int& i){
- l -= i;
- return *this;
- }
- };
- 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;
- cout << "Liczba robotow :" << robot::liczba_obiektow << endl;
- linia[0] = new rt1("polozono");
- linia[1] = new rt2;
- linia[2] = new rt1("polozono");
- linia[3] = new rt2;
- linia[4] = new rt1(odbior);
- cout << "Liczba robotow :" << robot::liczba_obiektow << endl;
- for(int i=0; i<5; ++i){
- try{
- cout << *linia[i];
- }catch(const string& e){cout << e << endl;}
- }
- cout << "********* 5 **********" << endl;
- for(int i=0; i<5; ++i) {
- delete linia[i];
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement