Advertisement
35657

Untitled

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