35657

Untitled

Feb 22nd, 2024 (edited)
1,042
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 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.     String(const int string_capacity) : size_(0), capacity_(string_capacity), str_(new char[capacity_]) {
  16.         total_number_string_++;
  17.     }
  18.  
  19.     String(const char* new_string) {
  20.         capacity_ = strlen(new_string) + 1;
  21.         str_ = new char[capacity_];
  22.         strcpy(str_, new_string);
  23.         size_ = capacity_ - 1;
  24.         total_number_string_++;
  25.     }
  26.  
  27.     String(const String& other) : size_(other.size_), capacity_(other.capacity_), str_(new char[capacity_]) {
  28.         strcpy(str_, other.str_);
  29.         total_number_string_++;
  30.     }
  31.  
  32.     void set_string(const char* new_string) {
  33.         int new_space = strlen(new_string) + 1;
  34.         if (capacity_ < new_space) {
  35.             delete[] str_;
  36.             capacity_ = new_space;
  37.             str_ = new char[capacity_];
  38.         }
  39.         strcpy(str_, new_string);
  40.         size_ = capacity_ - 1;
  41.     }
  42.  
  43.     const char* get_string() {
  44.         return str_;
  45.     }
  46.  
  47.     void set_char(const int index, const char ch) {
  48.         if (index < 0 || index >= size_) {
  49.             cout << "Некорректный индекс" << endl;
  50.             return;
  51.         }
  52.         str_[index] = ch;
  53.     }
  54.  
  55.     char get_char(const int index) {
  56.         if (index < 0 || index >= size_) {
  57.             cout << "Некорректный индекс" << endl;
  58.         }
  59.         return str_[index];
  60.     }
  61.  
  62.     int size() {
  63.         return size_;
  64.     }
  65.  
  66.     int capacity() {
  67.         return capacity_;
  68.     }
  69.  
  70.     static int get_total_number_strings() {
  71.         return total_number_string_;
  72.     }
  73.  
  74.     ~String() {
  75.         delete[] str_;
  76.         total_number_string_--;
  77.     }
  78.  
  79.  
  80. private:
  81.     int size_;
  82.     int capacity_;
  83.     char* str_;
  84.     static int total_number_string_;
  85. };
  86.  
  87. int String::total_number_string_ = 0;
  88.  
  89. int main() {
  90.     setlocale(LC_ALL, "ru");
  91.  
  92.     String str1;
  93.  
  94.     String str2(30);
  95.  
  96.     cout << String::get_total_number_strings() << endl;
  97.  
  98.     String str3("Привет");
  99.  
  100.     cout << str3.get_string() << endl;
  101.  
  102.     str3.set_string("Новая строка");
  103.  
  104.     cout << str3.get_string() << endl;
  105.  
  106.     str1.set_string("Еще одна новая строка");
  107.  
  108.     cout << str1.get_string() << endl;
  109.  
  110.     String str4(str3);
  111.  
  112.     cout << str4.get_string() << endl;
  113.  
  114.     cout << String::get_total_number_strings() << endl;
  115.  
  116. }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment