SHARE
TWEET

komunikator

a guest Jan 21st, 2020 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class komunikator {  
  6. public:
  7.     static int liczba_obiektow;
  8.     komunikator(){liczba_obiektow++; }
  9.     virtual ostream& wyswietl(ostream& out) const = 0;
  10.     virtual void operator+=(const string& a) const = 0;
  11.  
  12.  virtual ~komunikator(){ liczba_obiektow--; }
  13. };
  14.  
  15. int komunikator::liczba_obiektow = 0;
  16.  
  17. class k1 : public komunikator {
  18. protected:
  19.     string* txt;
  20. public:
  21.     k1():txt(new string("----")){}
  22.     k1(const string& a1): txt(new string(a1)) {}
  23.     k1(const k1&r): txt(new string(*r.txt)){}
  24.  
  25.     k1& operator=(const k1& r) {
  26.         if (this != &r) {
  27.             delete txt;
  28.             txt = new string(*r.txt);
  29.         }
  30.         return *this;
  31.     }
  32.  
  33.     void operator+=(const string& a)const {
  34.         *txt += a;
  35.     }
  36.  
  37.     ostream& wyswietl(ostream& out)const { return out << *txt; }
  38.     ~k1() { delete txt; }
  39. };
  40.  
  41. class k2 : public k1 {
  42.     int l;
  43. public:
  44.     k2():k1(), l(0) {}
  45.     k2(const string& a1, const int& a2) :k1(a1),l(a2){}
  46.  
  47.     ostream& wyswietl(ostream& out)const {
  48.         if (*txt == "----" && l == 0) throw string("brak danych\n");
  49.         return out << *txt<< " " << l; }
  50.  
  51.  
  52.  
  53.     friend k2& operator+=(k2& a1, const int& p);
  54.        
  55.  
  56.  
  57. };
  58.  
  59. k2& operator+=(k2& a1, const int& p){
  60.  
  61.     a1.l += p;
  62.     return a1;
  63. }
  64.  
  65.  
  66. ostream& operator<<(ostream& out, const komunikator& r) {
  67.     return r.wyswietl(out) << endl;
  68. }
  69.  
  70.  
  71.  
  72. int main()
  73. {
  74.     const k1 koniec("Koniec komunikatow");
  75.  
  76.     komunikator* linia[5];
  77.     linia[0] = new k1("Temperatura powietrza");
  78.     linia[1] = new k2("Czestochowa", -5);
  79.     linia[2] = new k1("Opady sniegu");
  80.     linia[3] = new k2("Katowice", 10);
  81.     linia[4] = new k1(koniec);
  82.  
  83.     for (int i = 0; i < 5; ++i)
  84.         cout << *linia[i];
  85.  
  86.     cout << "************* 3 ************" << endl;
  87.  
  88.     *linia[0] += " [ oC ]:";
  89.     *linia[2] += " [ cm ]:";
  90.     *(static_cast<k2*>(linia[1])) += 2;
  91.     *(static_cast<k2*>(linia[3])) += 2;
  92.  
  93.     for (int i = 0; i < 5; ++i) {
  94.         cout << *linia[i];
  95.         delete linia[i];
  96.     }
  97.        
  98.     cout << "************* 4 ************" << endl;
  99.  
  100.     cout << "Liczba dialajacych komunikatorow :"
  101.         << komunikator::liczba_obiektow << endl;
  102.  
  103.     linia[0] = new k1("Temperatura powietrza");
  104.     linia[1] = new k2;
  105.     linia[2] = new k1("Opady sniegu");
  106.     linia[3] = new k2;
  107.     linia[4] = new k1(koniec);
  108.  
  109.     cout << "Liczba dialajacych komunikatorow :"
  110.         << komunikator::liczba_obiektow << endl;
  111.  
  112.     for (int i = 0; i < 5; ++i) try{
  113.         cout << *linia[i];
  114.         delete linia[i];
  115.     }
  116.     catch (const string & r) { cout << r; }
  117.    
  118.     cout << "************* 5 ************" << endl;
  119.     return 0;
  120. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top