Advertisement
Pr0nly

g1_pp

Jan 14th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class komunikator {
  7. public:
  8.     static int liczba_obiektow;
  9.     komunikator() { liczba_obiektow++;}
  10.     virtual ostream& wyswietl(ostream& out) const = 0;
  11.     virtual ~komunikator() { liczba_obiektow--; }
  12. };
  13. int komunikator::liczba_obiektow = 0;
  14.  
  15. class k1: public komunikator {
  16. protected:
  17.     string *text;
  18.  
  19. public:
  20.     k1(): text(new string("brak")) {}
  21.     k1(const string& a): text(new string(a)) {}
  22.     k1(const k1& a): text(new string(*a.text)) {}
  23.  
  24.     k1& operator = (const k1& a) {
  25.         if(this != &a) {
  26.             delete text;
  27.             text = new string(*a.text);
  28.         }
  29.         return *this;
  30.     }
  31.  
  32.  
  33.     ~k1() { delete text; }
  34.  
  35.     ostream& wyswietl(ostream& out) const { return out << *text; }
  36. };
  37.  
  38. class k2: public k1 {
  39. protected:
  40.     int wartosc;
  41.  
  42. public:
  43.     k2(): k1(), wartosc(0) {}
  44.     k2(const string& a, const int& b): k1(a), wartosc(b) {}
  45.  
  46.     k2& operator += (const int& a) {
  47.         wartosc += a;
  48.     }
  49.  
  50.  
  51.     ostream& wyswietl(ostream& out) const {
  52.         if(*text == "brak" && wartosc == 0) throw string("Brak danych");
  53.         return out << *text << " " << wartosc;
  54.     }
  55.  
  56. };
  57.  
  58. ostream& operator << (ostream& out, const komunikator& k) {
  59.     return k.wyswietl(out) << endl;
  60. }
  61.  
  62.  
  63.  
  64. int main() {
  65.     const k1 koniec("Koniec komunikatow");
  66.  
  67.     komunikator* linia [5];
  68.     linia[0] = new k1("Temperatura powietrza:");
  69.     linia[1] = new k2("Czestochowa", -5);
  70.     linia[2] = new k1("Opady sniegu:");
  71.     linia[3] = new k2("Katowice", 10);
  72.     linia[4] = new k1(koniec);
  73.  
  74.     for(int i=0; i<5; ++i)
  75.         cout << *linia[i];
  76.  
  77.     cout << "************* 3 ************" << endl;
  78.  
  79.     *(static_cast<k2*>(linia[1])) += 2;
  80.     *(static_cast<k2*>(linia[3])) += 2;
  81.  
  82.     for(int i=0; i<5; ++i){
  83.         cout << *linia[i];
  84.         delete linia[i];
  85.     }
  86.  
  87.     cout << "************* 4 ************" << endl;
  88.  
  89.     cout << "Liczba dzialajacych obiektow: "
  90.          << komunikator::liczba_obiektow << endl;
  91.  
  92.     linia[0] = new k1("Temperatura powietrza:");
  93.     linia[1] = new k2;
  94.     linia[2] = new k1("Opady sniegu:");
  95.     linia[3] = new k2;
  96.     linia[4] = new k1(koniec);
  97.  
  98.     cout << "Liczba dzialajacych obiektow: "
  99.          << komunikator::liczba_obiektow << endl;
  100.  
  101.     for(int i=0; i<5; ++i)
  102.     try {
  103.         cout << *linia[i];
  104.     } catch(const string& e) { cout << e << endl; }
  105.  
  106.     cout << "************* 5 ************" << endl;
  107.  
  108.     for(int i=0; i<5; ++i)
  109.         delete linia[i];
  110.  
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement