Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- class komunikator {
- public:
- static int liczba_obiektow;
- komunikator() { liczba_obiektow++;}
- virtual ostream& wyswietl(ostream& out) const = 0;
- virtual ~komunikator() { liczba_obiektow--; }
- };
- int komunikator::liczba_obiektow = 0;
- class k1: public komunikator {
- protected:
- string *text;
- public:
- k1(): text(new string("brak")) {}
- k1(const string& a): text(new string(a)) {}
- k1(const k1& a): text(new string(*a.text)) {}
- k1& operator = (const k1& a) {
- if(this != &a) {
- delete text;
- text = new string(*a.text);
- }
- return *this;
- }
- ~k1() { delete text; }
- ostream& wyswietl(ostream& out) const { return out << *text; }
- };
- class k2: public k1 {
- protected:
- int wartosc;
- public:
- k2(): k1(), wartosc(0) {}
- k2(const string& a, const int& b): k1(a), wartosc(b) {}
- k2& operator += (const int& a) {
- wartosc += a;
- }
- ostream& wyswietl(ostream& out) const {
- if(*text == "brak" && wartosc == 0) throw string("Brak danych");
- return out << *text << " " << wartosc;
- }
- };
- ostream& operator << (ostream& out, const komunikator& k) {
- return k.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;
- *(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 dzialajacych obiektow: "
- << 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 dzialajacych obiektow: "
- << komunikator::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