Advertisement
Guest User

Untitled

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