Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class komunikator {
- public:
- static int liczba_obiektow;
- komunikator(){liczba_obiektow++; }
- virtual ostream& wyswietl(ostream& out) const = 0;
- virtual void operator+=(const string& a) const = 0;
- virtual ~komunikator(){ liczba_obiektow--; }
- };
- int komunikator::liczba_obiektow = 0;
- class k1 : public komunikator {
- protected:
- string* txt;
- public:
- k1():txt(new string("----")){}
- k1(const string& a1): txt(new string(a1)) {}
- k1(const k1&r): txt(new string(*r.txt)){}
- k1& operator=(const k1& r) {
- if (this != &r) {
- delete txt;
- txt = new string(*r.txt);
- }
- return *this;
- }
- void operator+=(const string& a)const {
- *txt += a;
- }
- ostream& wyswietl(ostream& out)const { return out << *txt; }
- ~k1() { delete txt; }
- };
- class k2 : public k1 {
- int l;
- public:
- k2():k1(), l(0) {}
- k2(const string& a1, const int& a2) :k1(a1),l(a2){}
- ostream& wyswietl(ostream& out)const {
- if (*txt == "----" && l == 0) throw string("brak danych\n");
- return out << *txt<< " " << l; }
- friend k2& operator+=(k2& a1, const int& p);
- };
- k2& operator+=(k2& a1, const int& p){
- a1.l += p;
- return a1;
- }
- ostream& operator<<(ostream& out, const komunikator& r) {
- return r.wyswietl(out) << endl;
- }
- int main()
- {
- const k1 koniec("Koniec komunikatow");
- komunikator* linia[5];
- linia[0] = new k1("Temperatura powietrza");
- linia[1] = new k2("Czestochowa", -5);
- linia[2] = new k1("Opady sniegu");
- linia[3] = new k2("Katowice", 10);
- linia[4] = new k1(koniec);
- for (int i = 0; i < 5; ++i)
- cout << *linia[i];
- cout << "************* 3 ************" << endl;
- *linia[0] += " [ oC ]:";
- *linia[2] += " [ cm ]:";
- *(static_cast<k2*>(linia[1])) += 2;
- *(static_cast<k2*>(linia[3])) += 2;
- for (int i = 0; i < 5; ++i) {
- cout << *linia[i];
- delete linia[i];
- }
- cout << "************* 4 ************" << endl;
- cout << "Liczba dialajacych komunikatorow :"
- << komunikator::liczba_obiektow << endl;
- linia[0] = new k1("Temperatura powietrza");
- linia[1] = new k2;
- linia[2] = new k1("Opady sniegu");
- linia[3] = new k2;
- linia[4] = new k1(koniec);
- cout << "Liczba dialajacych komunikatorow :"
- << komunikator::liczba_obiektow << endl;
- for (int i = 0; i < 5; ++i) try{
- cout << *linia[i];
- delete linia[i];
- }
- catch (const string & r) { cout << r; }
- cout << "************* 5 ************" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement