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 ~robot() { liczba_obiektow--; }
- virtual void praca() const = 0;
- virtual ostream& wyswietl(ostream& out) const = 0;
- };
- int robot::liczba_obiektow = 0;
- class rt1: public robot {
- protected:
- string *txt;
- public:
- rt1(): txt(new string("---")) {}
- rt1(const string& a): txt(new string(a)) {}
- rt1(const rt1& a): txt(new string(*a.txt)) {}
- rt1& operator = (const rt1& a) {
- if(this != &a) {
- delete txt;
- txt = new string(*a.txt);
- }
- return *this;
- }
- ~rt1() { delete txt; }
- ostream& wyswietl(ostream& out) const {
- return out << *txt << endl;
- }
- void praca() const { wyswietl(cout); }
- };
- class rt2: public rt1 {
- protected:
- int ilosc;
- public:
- rt2(): rt1(), ilosc() {}
- rt2(const string& a, const int& b): rt1(a), ilosc(b) {}
- ostream& wyswietl(ostream& out) const {
- if(*txt == "---" && ilosc == 0) throw string("robot niezaprogramowany");
- return out << *txt << " " << ilosc << endl;
- }
- void praca() const { wyswietl(cout); }
- rt2& operator -= (const int& i) {
- ilosc -= 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;
- 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;
- }
Add Comment
Please, Sign In to add comment