35657

Untitled

Jun 18th, 2023
929
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.20 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <cstring>
  5.  
  6.  
  7. using namespace std;
  8.  
  9. class String {
  10.  
  11. public:
  12.  
  13.     String() : size(0), capacity(15), str(new char[15]) {
  14.         total_number_strings++;
  15.     } // конструктор по умолчанию
  16.  
  17.     String(const int string_capacity) : size(0), capacity(string_capacity), str(new char[string_capacity]) {
  18.         total_number_strings++;
  19.     } // параметризованный конструктор для одного параметра типа int
  20.  
  21.     String(const char new_str[]) {
  22.         capacity = strlen(new_str) + 1;
  23.         str = new char[capacity];
  24.         strcpy(str, new_str);
  25.         size = capacity - 1;
  26.         total_number_strings++;
  27.     } // параметризованный конструктор для одного параметра типа char[]
  28.  
  29.     String(const String& other) : size(other.size), capacity(other.capacity), str(new char[capacity]) {
  30.         strcpy(str, other.str);
  31.         total_number_strings++;
  32.     } // конструктор копирования
  33.  
  34.     String& operator=(const String& other) {
  35.         if (&other != this) {
  36.             size = other.size;
  37.             capacity = other.capacity;
  38.             delete[] str;
  39.             str = new char[capacity];
  40.             strcpy(str, other.str);
  41.         }
  42.         return *this;
  43.     } // копирующий оператор присваивания = для другого экземпляра String
  44.  
  45.     String(String&& other) : size(other.size), capacity(other.capacity), str(other.str) {
  46.         other.str = nullptr;
  47.         other.size = 0;
  48.         other.capacity = 0;
  49.         total_number_strings++;
  50.     } // конструктор перемещения
  51.  
  52.     String& operator=(String&& other) {
  53.         if (&other != this) {
  54.             delete[] str;
  55.             size = other.size;
  56.             capacity = other.capacity;
  57.             str = other.str;
  58.             other.str = nullptr;
  59.             other.size = 0;
  60.             other.capacity = 0;
  61.         }
  62.         return *this;
  63.     } // перемещающий оператор присваивания
  64.  
  65.     char& operator[] (const int index) {
  66.         if (index < 0 || index >= size) {
  67.             cout << "Invalid index" << endl;
  68.             abort;
  69.         }
  70.         return str[index];
  71.     } // перегрузка оператора индексирования (сеттер)
  72.  
  73.     char operator[] (const int index) const {
  74.         if (index < 0 || index >= size) {
  75.             cout << "Invalid index" << endl;
  76.             abort;
  77.         }
  78.         return str[index];
  79.     } // перегрузка оператора индексирования для константных объектов
  80.  
  81.     bool operator==(const String& right) const {
  82.         if (right.size != size) {
  83.             return false;
  84.         }
  85.         for (int i = 0; i < size; i++) {
  86.             if (str[i] != right.str[i])
  87.                 return false;
  88.         }
  89.         return true;
  90.     }
  91.  
  92.     bool operator!=(const String& right) const {
  93.         return !(*this == right);
  94.     }
  95.  
  96.     int Size() const {
  97.         return size;
  98.     }
  99.  
  100.     int Capacity() const {
  101.         return capacity;
  102.     }
  103.  
  104.     static int GetTotalNumberStrings() {
  105.         return total_number_strings;
  106.     }
  107.  
  108.     ~String() {
  109.         delete[] str;
  110.         total_number_strings--;
  111.     }
  112.  
  113. private:
  114.     int size;
  115.     int capacity;
  116.     char* str;
  117.     static int total_number_strings;
  118. };
  119.  
  120. ostream& operator<<(ostream& output, const String& string) {
  121.     for (int i = 0; i < string.Size(); i++) {
  122.         output << string[i];
  123.     }
  124.     return output;
  125. } // перегрузка оператора вывода в поток <<
  126.  
  127. istream& operator>>(istream& input, String& string) {
  128.     int capacity = 15;
  129.     int i = 0;
  130.     char* str = new char[capacity];
  131.     char ch;
  132.     while (ch = input.get()) {
  133.         if (ch == ' ' || ch == '\n') {
  134.             break;
  135.         }
  136.         if (i == capacity - 1) {
  137.             char* temp = new char[capacity *= 2];
  138.             for (int j = 0; j <= i; j++) {
  139.                 temp[j] = str[j];
  140.             }
  141.             delete[] str;
  142.             str = temp;
  143.         }
  144.         str[i] = ch;
  145.         i++;
  146.     }
  147.     if (i > 0) {
  148.         str[i] = '\0';
  149.         string = move(String(str));
  150.     }
  151.     return input;
  152. } // перегрузка оператора ввода из потока >>
  153.  
  154. int String::total_number_strings = 0;
  155.  
  156.  
  157. int main() {
  158.     String my_string;
  159.     cout << "Size: " << my_string.Size() << " Capacity: " << my_string.Capacity() << endl;
  160.     my_string = "Hello";
  161.     cout << my_string << endl;
  162.     cout << "Size: " << my_string.Size() << " Capacity: " << my_string.Capacity() << endl;
  163.     my_string = "Hello my dear friend";
  164.     cout << my_string << endl;
  165.     cout << "Size: " << my_string.Size() << " Capacity: " << my_string.Capacity() << endl;
  166.     my_string[2] = 'm';
  167.     cout << my_string << endl;
  168.     cout << my_string[1] << endl;
  169.     String my_string2("1234");
  170.     cout << my_string2 << endl;
  171.     cout << "Size: " << my_string2.Size() << " Capacity: " << my_string2.Capacity() << endl;
  172.     const String my_string3(my_string);
  173.     cout << my_string3[1] << endl;
  174.     cout << my_string3 << endl;
  175.     cout << String::GetTotalNumberStrings() << endl;
  176.     cout << (my_string == my_string2) << endl;
  177.     cout << (my_string != my_string2) << endl;
  178.     String my_string4;
  179.     my_string4 = "Hello";
  180.     cout << my_string4 << endl;
  181.     String my_string5;
  182.     cin >> my_string5;
  183.     cout << my_string5 << endl;
  184.  
  185.     cout << "my_string4 " << my_string4 << endl;
  186.     String my_string6(move(my_string4));
  187.     cout << "my_string6 " << my_string6 << endl;
  188.     cout << "my_string4 " << my_string4 << endl;
  189.  
  190.     cout << "my_string " << my_string << endl;
  191.     my_string4 = move(my_string);
  192.     cout << "my_string4 " << my_string4 << endl;
  193.     cout << "my_string " << my_string << endl;
  194.  
  195. }
Advertisement
Add Comment
Please, Sign In to add comment