Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.30 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include<iostream>
  3. using namespace std;
  4.  
  5.  
  6. class STRING
  7. {
  8. private:
  9.     char *str;
  10.     int length;
  11. public:
  12.     STRING()
  13.     {
  14.         cout << "По умолчанию " << endl;
  15.         length = 0;
  16.         str = new char[1];
  17.         str[0] = '\0';
  18.     }
  19.     ~STRING()
  20.     {
  21.         cout << "Вызван деструктор " << this << endl;
  22.         delete[] this->str;
  23.     }
  24.     STRING(char *str)
  25.     {
  26.         cout << "Вызван конструктор с параметром char* " << this << endl;
  27.         length = strlen(str);
  28.         this->str = new char[length + 1];
  29.         for (int i = 0; i < length; i++)
  30.         {
  31.             this->str[i] = str[i];
  32.  
  33.         }
  34.         this->str[length] = '\0';
  35.     }
  36.     STRING(const STRING &other)
  37.     {
  38.         cout << "Вызван конструктор копирования " << endl;
  39.         length = other.length;
  40.         this->str = new char[length + 1];
  41.         for (int i = 0; i < length; i++)
  42.         {
  43.             this->str[i] = other.str[i];
  44.         }
  45.  
  46.         this->str[length] = '\0';
  47.     }
  48.     STRING& operator =(const STRING &other)
  49.     {
  50.         cout << "Вызван оператор присваивания " << endl;
  51.         if (this->str != '\0')
  52.         {
  53.             delete[] str;
  54.         }
  55.         length = other.length;
  56.         this->str = new char[length + 1];
  57.         for (int i = 0; i < length; i++)
  58.         {
  59.             this->str[i] = other.str[i];
  60.         }
  61.         this->str[length] = '\0';
  62.  
  63.         return *this;
  64.  
  65.     }
  66.  
  67.     operator const char*() const
  68.     {
  69.         cout << "Приведение типа " << endl;
  70.         return str;
  71.     }
  72.  
  73.     STRING operator +(const STRING &other)
  74.     {
  75.         cout << "Вызван оператор + " << endl;
  76.         STRING newStr;
  77.         newStr.length = this->length + other.length;
  78.         newStr.str = new char[newStr.length + 1];
  79.         int i = 0;
  80.         for (; i < this->length; i++)
  81.         {
  82.             newStr.str[i] = this->str[i];
  83.         }
  84.         for (int j = 0; j < other.length; j++, i++)
  85.         {
  86.             newStr.str[i] = other.str[j];
  87.         }
  88.         newStr.str[newStr.length] = '\0';
  89.         return newStr;
  90.     };
  91.     STRING& operator +=(const STRING &other)
  92.     {
  93.         cout << "Вызван оператор += " << endl;
  94.         STRING newStr;
  95.         newStr.length = this->length + other.length;
  96.         newStr.str = new char[newStr.length + 1];
  97.         int i = 0;
  98.         for (; i<this->length; i++)
  99.         {
  100.             newStr.str[i] = this->str[i];
  101.         }
  102.         for (int j = 0; j< other.length; j++, i++)
  103.         {
  104.             newStr.str[i] = other.str[j];
  105.             ;
  106.         }
  107.         newStr.str[newStr.length] = '\0';
  108.         delete[] str;
  109.         length = newStr.length;
  110.         this->str = new char[length + 1];
  111.         for (int i = 0; i < length; i++)
  112.         {
  113.             this->str[i] = newStr.str[i];
  114.         }
  115.         this->str[length] = '\0';
  116.         return *this;
  117.     }
  118.     char& operator [](int index)
  119.     {
  120.         return this->str[index];
  121.     }
  122.  
  123.     void Print()
  124.     {
  125.         cout << str;
  126.         cout << endl;
  127.     }
  128. };
  129. int main()
  130. {
  131.     setlocale(LC_ALL, "rus");
  132.     STRING a("Pupa");
  133.     STRING b("Lupa");
  134.     STRING c(b);
  135.     cout << c << endl;
  136.     STRING result;
  137.     cout << "============================================================" << endl << endl;
  138.     result = a + b;
  139.     cout << result << endl;
  140.     cout << "============================================================" << endl << endl;
  141.     a = b;
  142.     cout << a << endl;
  143.     cout << "============================================================" << endl << endl;
  144.     c += a;
  145.     cout << c << endl;
  146.     cout << "============================================================" << endl << endl;
  147.     cout << a[0] << endl;
  148.     cout << a << endl;
  149.     a[0] = 'P';
  150.     cout << a << endl;
  151.     system("pause");
  152.     return 0;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement