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 void zamien(const int& p) = 0;
- friend const robot& operator-=(robot&r,const int& p);
- virtual ~robot() { liczba_obiektow--; }
- };
- const robot& operator-=(robot& r, const int& p) {
- r.zamien(p);
- return r;
- }
- int robot::liczba_obiektow = 0;
- class rt1 :public robot {
- protected:
- string * txt;
- public:
- rt1():txt(new string("brak")){}
- rt1(const string& a1) :txt(new string(a1)) {}
- rt1(const rt1& r) :txt(new string(*r.txt)) {}
- rt1& operator=(const rt1& r) {
- if (this != &r) {
- delete txt;
- txt = new string(*r.txt);
- }return *this;
- }
- void praca()const {
- cout << *txt<<endl;
- }
- void zamien(const int& p){}
- ostream& wyswietl(ostream& out)const { return out << *txt; }
- ~rt1() { delete txt; }
- };
- class rt2 :public rt1 {
- int w;
- public:
- rt2():rt1(),w(0){}
- rt2(const string& a1,const int& a2) :rt1(a1), w(a2) {}
- rt2(const rt2& r) :rt1(*r.txt),w(r.w) {}
- void praca()const {
- cout << *txt << " " << w << endl;
- }
- void zamien(const int& p) {
- w -= p;
- }
- ostream& wyswietl(ostream& out)const {
- if (*txt == "brak" && w == 0)throw string("robot niezaporogramowany");
- return out << *txt << " " << w; }
- };
- ostream& operator<<(ostream& out, const robot& r) {
- return r.wyswietl(out) << endl;
- }
- 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 rt2("prawo", 4);
- 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];
- delete linia[i];
- }catch (const string e) { cout << e <<endl; }
- cout << "********5**********" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement