SHARE
TWEET

k2

Pr0nly Jan 21st, 2020 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  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.     virtual ~komunikator(){liczba_obiektow--;}
  12. };
  13. int komunikator::liczba_obiektow = 0;
  14.  
  15. class k1 : public komunikator{
  16. protected:
  17.     string * t;
  18. public:
  19.     k1(): t(new string("---")){}
  20.     k1(const string& a1): t(new string(a1)){}
  21.     k1(const k1& r): t(new string(*r.t)){}
  22.  
  23.     k1& operator=(const k1& r){
  24.         if(this!=&r){
  25.             delete t;
  26.             t = new string(*r.t);
  27.         }
  28.         return *this;
  29.     }
  30.  
  31.     void operator+=(const string& a) const{
  32.         *t += a;
  33.     }
  34.  
  35.     ostream& wyswietl(ostream& out)const {return out << *t;}
  36. ~k1(){delete t;}
  37. };
  38.  
  39. class k2 : public k1{
  40. int l;
  41. public:
  42.     k2(): l(0){}
  43.     k2(const string& a1, const int& a2): k1(a1), l(a2){}
  44.    
  45.     ostream& wyswietl(ostream& out)const {
  46.     if(*t == "---" && l == 0)throw string("Brak danych\n");
  47.     return out << *t << ' ' << l;}
  48. };
  49.  
  50. ostream& operator<<(ostream& out, const komunikator& r){
  51.     return r.wyswietl(out) << endl;
  52. }
  53.  
  54. int main(){
  55.     const k1 koniec("Koniec komunikatow");
  56.     komunikator *linia[5];
  57.     linia[0] = new k1("Temperatury powietrza");
  58.     linia[1] = new k2("Czestochowa", -5);
  59.     linia[2] = new k1("Opady sniegu");
  60.     linia[3] = new k2("Katowice", 10);
  61.     linia[4] = new k1(koniec);
  62.  
  63.     for(int i = 0; i < 5; i++){
  64.         cout << *linia[i];
  65.    }
  66.  
  67.     cout << "************* 3 ************" << endl;
  68.  
  69.  
  70.     *linia[0] += " [oC]:";
  71.     *linia[2] += " [cm]:";
  72.  
  73.     for(int i = 0; i < 5; i++){
  74.         cout << *linia[i];
  75.         delete linia[i];
  76.    }
  77.     cout << "************* 4 ************" << endl;
  78.    
  79.     cout << "Liczba dzialajacych komunikatorow: " << komunikator::liczba_obiektow << endl;
  80.  
  81.     linia[0] = new k1("Temperatury powietrza");
  82.     linia[1] = new k2;
  83.     linia[2] = new k1("Opady sniegu");
  84.     linia[3] = new k2;
  85.     linia[4] = new k1(koniec);
  86.  
  87.     cout << "Liczba dzialajacych komunikatorow: " << komunikator::liczba_obiektow << endl;
  88.  
  89.     for(int i = 0; i < 5; i++){
  90.         try{cout << *linia[i];}catch(const string& e){cout << e;}
  91.         delete linia[i];
  92.     }
  93.     return 0;
  94. }
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