Advertisement
35657

Untitled

Feb 27th, 2024 (edited)
1,177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.14 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& operator=(const String& other) {
  30.         if (&other != this) { // проверка на самоприсваивание
  31.             size_ = other.size_;
  32.             capacity_ = other.capacity_;
  33.             delete[] str_;
  34.             str_ = new char[capacity_];
  35.             strcpy(str_, other.str_);
  36.         }
  37.         return *this;
  38.     } // переопределение копирующего оператора присваивания для String
  39.  
  40.     String& operator=(const char* str) {
  41.         size_ = strlen(str);
  42.         capacity_ = size_ + 1;
  43.         delete[] str_;
  44.         str_ = new char[capacity_];
  45.         strcpy(str_, str);
  46.         return *this;
  47.     } // переопределение копирующего оператора присваивания строкового литерала
  48.  
  49.     const char* get_string() const {
  50.         return str_;
  51.     }
  52.  
  53.  
  54.     int size() const {
  55.         return size_;
  56.     }
  57.  
  58.     int capacity() const {
  59.         return capacity_;
  60.     }
  61.  
  62.     bool operator==(const String& other) const  {
  63.         return !strcmp(str_, other.str_);
  64.     }
  65.  
  66.     bool operator!=(const String& other) const {
  67.         return strcmp(str_, other.str_);
  68.     }
  69.  
  70.     char& operator[](const int index) {
  71.         if (index < 0 || index >= size_) {
  72.             cout << "Некорректный индекс" << endl;
  73.         }
  74.         return str_[index];
  75.     }
  76.  
  77.     char operator[](const int index) const {
  78.         if (index < 0 || index >= size_) {
  79.             cout << "Некорректный индекс" << endl;
  80.         }
  81.         return str_[index];
  82.     }
  83.  
  84.     static int get_total_number_strings() {
  85.         return total_number_string_;
  86.     }
  87.  
  88.     ~String() {
  89.         delete[] str_;
  90.         total_number_string_--;
  91.     }
  92.  
  93.  
  94. private:
  95.     int size_;
  96.     int capacity_;
  97.     char* str_;
  98.     static int total_number_string_;
  99. };
  100.  
  101. ostream& operator<<(ostream& output, const String& string) {
  102.     output << string.get_string();
  103.     return output;
  104. } // переопределение оператора вывода в поток
  105.  
  106. istream& operator>>(istream& input, String& string) {
  107.     int capacity = 15;
  108.     int i = 0;
  109.     char* str = new char[capacity];
  110.     char ch;
  111.     while (ch = input.get()) {
  112.         if (ch == ' ' || ch == '\n') {
  113.             break;
  114.         }
  115.         if (i == capacity - 1) {
  116.             char* temp = new char[capacity *= 2];
  117.             for (int j = 0; j < i; j++) {
  118.                 temp[j] = str[j];
  119.             }
  120.             delete[] str;
  121.             str = temp;
  122.         }
  123.         str[i] = ch;
  124.         i++;
  125.     }
  126.     str[i] = '\0';
  127.     string = str;
  128.     return input;
  129. } // переопределение оператора чтения из потока
  130.  
  131.  
  132.  
  133. int String::total_number_string_ = 0;
  134.  
  135. int main() {
  136.     setlocale(LC_ALL, "ru");
  137.  
  138.     String str1;
  139.  
  140.     String str2(30);
  141.  
  142.     cout << String::get_total_number_strings() << endl;
  143.  
  144.     String str3("Привет");
  145.  
  146.     cout << str3 << endl;
  147.  
  148.     str3 = "Новая строка";
  149.  
  150.     cout << str3 << endl;
  151.  
  152.     str1 = "Еще одна новая строка";
  153.  
  154.     cout << str1 << endl;
  155.  
  156.     String str4(str3);
  157.  
  158.     cout << str3 << endl;
  159.  
  160.     cout << str4 << endl;
  161.  
  162.     cout << (str1 == str4) << endl;
  163.     cout << (str3 == str4) << endl;
  164.     cout << (str1 != str4) << endl;
  165.     cout << (str3 != str4) << endl;
  166.  
  167.     String str5;
  168.     str5 = str1;
  169.     cout << str5 << endl;
  170.     str5 = str5;
  171.     str5[0] = 'Т';
  172.     cout << str5 << endl;
  173.  
  174.     const String str7 = "Привет";
  175.  
  176.     cout << str7[0] << endl;
  177.  
  178.     cout << str7 << endl;
  179.  
  180.     String str8;
  181.  
  182.     cin >> str8;
  183.  
  184.     cout << str8;
  185. }
  186.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement