35657

Untitled

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