Advertisement
35657

Untitled

Jun 5th, 2023
884
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.72 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.     }
  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.     }
  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.     } // перегрузка оператора присваивания =
  44.  
  45.  
  46.  
  47.     String(String&& other) : size(other.size), capacity(other.capacity), str(other.str) {
  48.         other.str = nullptr;
  49.         other.size = 0;
  50.         other.capacity = 0;
  51.         total_number_strings++;
  52.     }
  53.  
  54.     String& operator=(String&& other) {
  55.         if (&other != this) {
  56.             size = other.size;
  57.             capacity = other.capacity;
  58.             delete[] str;
  59.             str = other.str;
  60.             other.str = nullptr;
  61.             other.size = 0;
  62.             other.capacity = 0;
  63.         }
  64.         return *this;
  65.     } // перегрузка оператора присваивания =
  66.  
  67.  
  68.     char& operator[] (const int index) {
  69.         if (index < 0 || index >= size) {
  70.             cout << "Invalid index" << endl;
  71.             abort;
  72.         }
  73.         return str[index];
  74.     } // перегрузка оператора индексирования (сеттер)
  75.  
  76.     char operator[] (const int index) const {
  77.         if (index < 0 || index >= size) {
  78.             cout << "Invalid index" << endl;
  79.             abort;
  80.         }
  81.         return str[index];
  82.     } // перегрузка оператора индексирования для константных объектов
  83.  
  84.     bool operator==(const String& right) const {
  85.         if (right.size != size) {
  86.             return false;
  87.         }
  88.         for (int i = 0; i < size; i++) {
  89.             if (str[i] != right.str[i])
  90.                 return false;
  91.         }
  92.         return true;
  93.     }
  94.  
  95.     bool operator!=(const String& right) const {
  96.         return !(*this == right);
  97.     }
  98.  
  99.     int Size() const {
  100.         return size;
  101.     }
  102.  
  103.     int Capacity() const {
  104.         return capacity;
  105.     }
  106.  
  107.     static int GetTotalNumberStrings() {
  108.         return total_number_strings;
  109.     }
  110.  
  111.     ~String() {
  112.         delete[] str;
  113.         total_number_strings--;
  114.     }
  115.  
  116. private:
  117.     int size;
  118.     int capacity;
  119.     char* str;
  120.     static int total_number_strings;
  121. };
  122.  
  123. ostream& operator<<(ostream& output, const String& string) {
  124.     for (int i = 0; i < string.Size(); i++) {
  125.         output << string[i];
  126.     }
  127.     return output;
  128. } // перегрузка оператора вывода в поток <<
  129.  
  130. istream& operator>>(istream& input, String& string) {
  131.     int capacity = 15;
  132.     int i = 0;
  133.     char* str = new char[capacity];
  134.     char ch;
  135.     while (ch = input.get()) {
  136.         if (ch == ' ' || ch == '\n') {
  137.             break;
  138.         }
  139.         if (i == capacity - 1) {
  140.             char* temp = new char[capacity *= 2];
  141.             for (int j = 0; j <= i; j++) {
  142.                 temp[j] = str[j];
  143.             }
  144.             delete[] str;
  145.             str = temp;
  146.         }
  147.         str[i] = ch;
  148.         i++;
  149.     }
  150.     if (i > 0) {
  151.         str[i] = '\0';
  152.         string = move(String(str));
  153.     }
  154.     return input;
  155. } // перегрузка оператора ввода из потока >>
  156.  
  157. int String::total_number_strings = 0;
  158.  
  159.  
  160. int main() {
  161.     String my_string;
  162.     cout << "Size: " << my_string.Size() << " Capacity: " << my_string.Capacity() << endl;
  163.     my_string = "Hello";
  164.     cout << my_string << endl;
  165.     cout << "Size: " << my_string.Size() << " Capacity: " << my_string.Capacity() << endl;
  166.     my_string = "Hello my dear friend";
  167.     cout << my_string << endl;
  168.     cout << "Size: " << my_string.Size() << " Capacity: " << my_string.Capacity() << endl;
  169.     my_string[2] = 'm';
  170.     cout << my_string << endl;
  171.     cout << my_string[1] << endl;
  172.     String my_string2("1234");
  173.     cout << my_string2 << endl;
  174.     cout << "Size: " << my_string2.Size() << " Capacity: " << my_string2.Capacity() << endl;
  175.     const String my_string3(my_string);
  176.     cout << my_string3[1] << endl;
  177.     cout << my_string3 << endl;
  178.     cout << String::GetTotalNumberStrings() << endl;
  179.     cout << (my_string == my_string2) << endl;
  180.     cout << (my_string != my_string2) << endl;
  181.     String my_string4;
  182.     my_string4 = my_string;
  183.     cout << my_string4 << endl;
  184.     my_string4 = "Hello";
  185.     cout << my_string4 << endl;
  186.     String my_string5;
  187.     cin >> my_string5;
  188.     cout << my_string5 << endl;
  189.  
  190.  
  191.     String my_string6(move(my_string4));
  192.     cout << my_string6 << endl;
  193.     cout << my_string4 << "clear" << endl;
  194.  
  195.     my_string4 = move(my_string);
  196.     cout << my_string4 << endl;
  197.     cout << my_string << "clear" << endl;
  198.  
  199.  
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement