Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. class K1 {
  8.     friend ostream & operator<<(ostream & stream, const K1 & obj);
  9.     string * p1;
  10. public:
  11.     K1() : p1(new string("Brak")) {}
  12.     K1(const K1 & obj): p1(new string(*obj.p1)) {}
  13.     K1(const string & str) : p1(new string(str)) {}
  14.     ~K1() { delete p1; }
  15.  
  16.     K1 & operator=(const K1 & obj);
  17.  
  18.     string *& getP1() { return p1; }
  19.  
  20.     const string & getP1() const { return *p1; }
  21.  
  22.     friend class K2;
  23. };
  24.  
  25. K1 & K1::operator=(const K1 & obj) {
  26.     if (this != &obj) {
  27.         *p1 = *obj.p1;
  28.     }
  29.  
  30.     return *this;
  31. }
  32.  
  33. ostream & operator<<(ostream & stream, const K1 & obj) {
  34.     return stream << *obj.p1;
  35. }
  36.  
  37. class K2 {
  38.     K1 txt[2];
  39.     int w1;
  40. public:
  41.     K2() {
  42.         *txt[0].p1 = "Brak";
  43.         *txt[1].p1 = "Brak";
  44.  
  45.         w1 = 0;
  46.     }
  47.     K2(const string & str1, const string & str2, const int & p) {
  48.         *txt[0].p1 = str1;
  49.         *txt[1].p1 = str2;
  50.  
  51.         w1 = p;
  52.     }
  53.     K2(const K2 & obj) {
  54.         *txt[0].p1 = *obj.txt[0].p1;
  55.         *txt[1].p1 = *obj.txt[1].p1;
  56.  
  57.         w1 = obj.w1;
  58.     }
  59.    
  60.     K2 & operator=(const K2 & obj);
  61.  
  62.     K2 & operator+=(const int & val);
  63.  
  64.     friend ostream & operator<<(ostream & stream, const K2 & obj);
  65.     friend istream & operator>>(istream & stream, K2 & obj);
  66.  
  67.     friend K2 operator+(const K2 & obj, const int & val);
  68.     friend K2 operator+(const K2 & obj, const string & val);
  69.     friend K2 operator+(const string & val, const K2 & obj);
  70. };
  71.  
  72. K2 operator+(const K2 & obj, const int & val) {
  73.     K2 newObj = K2(obj);
  74.     newObj.w1 += val;
  75.  
  76.     return newObj;
  77. }
  78.  
  79. K2 operator+(const K2 & obj, const string & val) {
  80.     K2 newObj = K2(obj);
  81.     *newObj.txt[1].getP1() += val;
  82.  
  83.     return newObj;
  84. }
  85.  
  86. K2 operator+(const string & val, const K2 & obj) {
  87.     K2 newObj = K2(obj);
  88.  
  89.     string temp = val;
  90.     temp += *newObj.txt[1].getP1();
  91.  
  92.     *newObj.txt[1].getP1() = temp;
  93.  
  94.     return newObj;
  95.  
  96.     return newObj;
  97. }
  98.  
  99. K2 operator+(const int & val, const K2 & obj) {
  100.     return obj + val;
  101. }
  102.  
  103. K2 & K2::operator=(const K2 & obj) {
  104.     if (this != &obj) {
  105.         *txt[0].p1 = *obj.txt[0].p1;
  106.         *txt[1].p1 = *obj.txt[1].p1;
  107.  
  108.         w1 = obj.w1;
  109.     }
  110.  
  111.     return *this;
  112. }
  113.  
  114. K2 & K2::operator+=(const int & val) {
  115.     w1 += val;
  116.  
  117.     return *this;
  118. }
  119.  
  120. istream & operator >> (istream & stream, K2 & obj) {
  121.     if (!stream) throw string("Strumien nie jest otwarty lub jest uszkodzony!");
  122.  
  123.     if (!(stream >> *obj.txt[0].getP1())) throw string("Blad podczas wyprowadzania z strumienia!");
  124.     if (!(stream >> *obj.txt[1].getP1())) throw string("Blad podczas wyprowadzania z strumienia!");
  125.     if (!(stream >> obj.w1)) throw string("Blad podczas wyprowadzania z strumienia!");
  126.  
  127.     if (!stream) throw string("Strumien jest uszkodzony!");
  128.  
  129.     return stream;
  130. }
  131.  
  132. ostream & operator<<(ostream & stream, const K2 & obj) {
  133.     return stream << obj.txt[0] << " " << obj.txt[1] << " " << obj.w1 << endl;
  134. }
  135.  
  136. int main()
  137. {
  138.     K2 ob1, ob2;
  139.  
  140.     const K2 * wsk1 = new K2("Ala", "Koala", 15);
  141.     const K2 ob3(*wsk1);
  142.     delete wsk1;
  143.     wsk1 = 0;
  144.  
  145.     const K2 * wsk2 = new K2(ob3);
  146.     ob2 = *wsk2;
  147.  
  148.     cout << ob1 << *wsk2;
  149.     delete wsk2;
  150.     wsk2 = 0;
  151.  
  152.     cout << ob2;
  153.     cout << ob2 + 10;
  154.  
  155.     cout << "***********   3   *************\n" << endl;
  156.  
  157.     K2 tab[4];
  158.  
  159.     ifstream file("data.txt", ios::out);
  160.     if (file) {
  161.         try {
  162.             for (int i = 0; i < 4; i++) {
  163.                 file >> tab[i];
  164.             }
  165.         } catch (string err) {
  166.             cout << err << endl;
  167.  
  168.             return 1;
  169.         }
  170.     }
  171.     else {
  172.         cerr << "Blad podczas otwierania strumienia!" << endl;
  173.  
  174.         return 1;
  175.     }
  176.  
  177.     for (int i = 0; i < 4; i++) {
  178.         tab[i] += 1;
  179.         cout << tab[i];
  180.     }
  181.  
  182.     cout << "***********   4   *************\n" << endl;
  183.  
  184.     tab[1] = tab[1] + " Kowalska";
  185.     tab[3] = "Bocian " + tab[3];
  186.  
  187.     for (int i = 0; i < 4; i++) {
  188.         cout << tab[i];
  189.     }
  190.  
  191.     cout << "***********   5   *************\n" << endl;
  192.     return 0;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement