Advertisement
Guest User

kolo1_2

a guest
Nov 12th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. class K2;
  5.  
  6. class K1{
  7.     string *p1;
  8. public:
  9.     K1(): p1(new string("---")){}
  10.     K1(const string& a1): p1(new string(a1)){}
  11.     //K1(const K1& r): p1(new string(*r.p1)){}  //konstruktor kopiujący
  12.     K1& operator =(const K1& r){
  13.         if(this != &r){
  14.             *p1 = *r.p1;
  15.         }
  16.         return *this;
  17.     }
  18.  
  19.     string& pp(){return *p1;};
  20.     const string& pp() const{return *p1;}
  21.  
  22.     friend ostream& operator <<(ostream&, const K1&);
  23.     //friend K2;
  24.     ~K1(){delete p1;}
  25. };
  26.  
  27.  
  28.  
  29. class K2{
  30.     K1 txt[2];
  31.     int w1;
  32. public:
  33.     K2(): w1(0){}
  34.     K2(const string& a1, const string& a2, const int& a3): w1(a3){
  35.         txt[0] = a1;
  36.         txt[1] = a2;}
  37.     K2(const K2& r): w1(r.w1){
  38.         txt[0] = r.txt[0];
  39.         txt[1] = r.txt[1];
  40.     }
  41.  
  42.     /*K2& operator=(const K2& r){           //przeciazenie operatora do wskaznikow
  43.         if(this != &r){
  44.             txt[0] = r.txt[0];
  45.             txt[1] = r.txt[1];
  46.             w1 = r.w1;
  47.         }
  48.         return *this;
  49.     }*/
  50.     friend ostream& operator <<(ostream&, const K2&);
  51.     friend K2 operator+(const K2&, const string&);
  52.     friend K2 operator+(const string&, const K2&);
  53.     friend istream& operator >>(istream&, K2&);
  54.     /*const K1& operator[](size_t i)const { //indeksowanie
  55.         return txt[i];
  56.     }
  57.     K1& operator[](size_t i){
  58.         return txt[i];
  59.     }*/
  60.  
  61.     K2 operator+(const int& l){
  62.         return K2(txt[0].pp(), txt[1].pp(), (w1+l));
  63.     }
  64.  
  65.     K2& operator+=(const int& l){
  66.         w1 += l;
  67.         return *this;
  68.     }
  69. };
  70. ostream& operator <<(ostream& out, const K1& r){return out << *r.p1;}
  71. ostream& operator <<(ostream& out, const K2& r){
  72.     return out << r.txt[0] << ' ' << r.txt[1] << ' ' << r.w1 << endl;
  73. }
  74.  
  75. istream& operator >>(istream& in, K2& r){
  76.     if(!in) throw string("Blad na strumieniu");
  77.  
  78.     if(!(in >> r.txt[0].pp())) throw string("blad wczytywania");
  79.     if(!(in >> r.txt[1].pp())) throw string("blad wczytywania");
  80.     if(!(in >> r.w1)) throw string("blad wczytywania");
  81.    
  82.     if(!in) throw string("Blad na strumieniu");
  83.  
  84.     return in;
  85. }
  86.  
  87. K2 operator+(const K2& l, const string& p){
  88.     return K2(l.txt[0].pp(), (l.txt[1].pp() + p), l.w1);
  89. }
  90. K2 operator+(const string& l, const K2& p){
  91.     return K2(p.txt[0].pp(), (l + p.txt[1].pp()), p.w1);
  92. }
  93.  
  94. int main(){
  95.     K2 ob1, ob2;
  96.     const K2* wsk1 = new K2("Ala", "Koala", 15);
  97.     const K2 ob3(*wsk1);
  98.     delete wsk1;
  99.     wsk1 = 0;
  100.  
  101.     const K2 * wsk2 = new K2(ob3);
  102.     ob2 = *wsk2;
  103.     cout << ob1 << *wsk2;
  104.     delete wsk2;
  105.     wsk2 = 0;
  106.     cout << ob2;
  107.     cout << ob2+10;
  108.  
  109.     cout << "\n**** 3 ****\n"<< endl;
  110.  
  111.     K2 tab[4];
  112.  
  113.     ifstream plik("data.txt");
  114.  
  115.     try{
  116.         for(int i = 0; i < 4; i++){
  117.             plik >> tab[i];
  118.         }
  119.     }
  120.     catch(string err){cerr << err;}
  121.     catch(...){cout << "blad nieznany";}
  122.     plik.close();
  123.  
  124.     for(int i = 0; i < 4; i++){
  125.         tab[i] += 1;
  126.         cout << tab[i];
  127.     }
  128.    
  129.     cout << "\n**** 4 ****\n"<< endl;
  130.  
  131.     tab[1] = tab[1] + " Kowalska";
  132.     tab[3] = "Bocian " + tab[3];
  133.  
  134.     for(int i = 0; i < 4; i++){
  135.         cout << tab[i];
  136.     }
  137.    
  138.     cout << "\n**** 5 ****\n"<< endl;
  139.    
  140.     return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement