Advertisement
Neveles

Untitled

Dec 1st, 2019
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. #include "MyString.h"
  2.  
  3. // конструкторы, деструктор
  4. MyString::MyString()
  5. {
  6.     str = nullptr;
  7.     length = 0;
  8. }
  9. MyString::MyString(const char* other)
  10. {
  11.     length = strlen(other);
  12.     this->str = new char[length + 1];
  13.  
  14.     for (int i = 0; i < length; i++)
  15.     {
  16.         this->str[i] = other[i];
  17.     }
  18.  
  19.     this->str[length] = '\0';
  20. }
  21. MyString::MyString(const MyString& other)
  22. {
  23.     length = strlen(other.str);
  24.     this->str = new char[length + 1];
  25.  
  26.     for (int i = 0; i < length; i++)
  27.     {
  28.         this->str[i] = other.str[i];
  29.     }
  30.  
  31.     this->str[length] = '\0';
  32. }
  33. MyString::MyString(MyString&& other)
  34. {
  35.     this->length = other.length;
  36.     this->str = other.str;
  37.     other.str = nullptr;
  38. }
  39. MyString::~MyString()
  40. {
  41.     delete[] this->str;
  42. }
  43.  
  44. // перегрузка присваивания и конкатенации
  45. //MyString& MyString::operator= (const MyString& other)
  46. //{
  47. //  if (this->str != nullptr)
  48. //  {
  49. //      delete[] str;
  50. //  }
  51. //
  52. //  length = strlen(other.str);
  53. //  this->str = new char[length + 1];
  54. //
  55. //  for (int i = 0; i < length; i++)
  56. //  {
  57. //      this->str[i] = other.str[i];
  58. //  }
  59. //
  60. //  this->str[length] = '\0';
  61. //
  62. //  return *this;
  63. //}
  64. MyString& MyString::operator=(const MyString& other)
  65. {
  66.     if (length != other.length) {
  67.         length = other.length;
  68.         delete[] str;
  69.         str = new char[length + 1];
  70.     }
  71.     strcpy_s(str, length + 1, other.str);
  72.     return *this;
  73. }
  74. MyString MyString::operator+ (const MyString& other)
  75. {
  76.     MyString newStr;
  77.  
  78.     int thisLength = strlen(this->str);
  79.     int otherLength = strlen(other.str);
  80.  
  81.     newStr.length = thisLength + otherLength;
  82.  
  83.     newStr.str = new char[thisLength + otherLength + 1];
  84.  
  85.     int i = 0;
  86.     for (; i < thisLength; i++)
  87.     {
  88.         newStr.str[i] = this->str[i];
  89.     }
  90.  
  91.     for (int j = 0; j < otherLength; j++, i++)
  92.     {
  93.         newStr.str[i] = other.str[j];
  94.     }
  95.  
  96.     newStr.str[thisLength + otherLength] = '\0';
  97.  
  98.     return newStr;
  99. }
  100.  
  101. //MyString& MyString::operator+= (const char& symbol)
  102. //{
  103. //  this->str[length] = symbol;
  104. //  ++length;
  105. //  str[length] = '\0';
  106. //  return *this;
  107. //}
  108.  
  109. // перегрузка операторов сравнения
  110. bool MyString::operator== (const MyString& other)
  111. {
  112.     if (this->length != other.length)
  113.     {
  114.         return false;
  115.     }
  116.  
  117.     for (int i = 0; i < this->length; i++)
  118.     {
  119.         if (this->str[i] != other.str[i])
  120.         {
  121.             return false;
  122.         }
  123.     }
  124.     return true;
  125. }
  126. bool MyString::operator!= (const MyString& other)
  127. {
  128.     return !(this->operator== (other));
  129. }
  130. bool MyString::operator> (const MyString& other)
  131. {
  132.     return (this->length > other.length);
  133. }
  134. bool MyString::operator< (const MyString& other)
  135. {
  136.     return (this->length < other.length);
  137. }
  138. bool MyString::operator>= (const MyString& other)
  139. {
  140.     return !(this->operator< (other));
  141. }
  142. bool MyString::operator<= (const MyString& other)
  143. {
  144.     return !(this->operator> (other));
  145. }
  146.  
  147. // перегрузка оператора индексирования
  148. char& MyString::operator[] (int index)
  149. {
  150.     return this->str[index];
  151. }
  152.  
  153. // перегрузка ввода/вывода, как дружественные функции
  154. ostream& operator<< (ostream& out, const MyString& other)
  155. {
  156.     out << other.str;
  157.     return out;
  158. }
  159. istream& operator>> (istream& in, MyString& other)
  160. {
  161.     char BUFF[2048];
  162.  
  163.     in.getline(BUFF, sizeof BUFF);
  164.     other = BUFF;
  165.  
  166.     return in;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement