35657

Untitled

Jun 15th, 2024
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 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.  
  13.     explicit String(const int string_capacity) : size_(0), capacity_(string_capacity), str_(new char[capacity_]) {}
  14.  
  15.     String(const char* new_string) : size_(strlen(new_string)), capacity_(size_ + 1), str_(new char[capacity_]) {
  16.         strcpy(str_, new_string);}
  17.  
  18.     String(const String& other) : size_(other.size_), capacity_(other.capacity_), str_(new char[capacity_]) {
  19.         strcpy(str_, other.str_);}
  20.  
  21.     String& operator=(const String& other) {
  22.         if (&other != this) {
  23.             size_ = other.size_;
  24.             capacity_ = other.capacity_;
  25.             delete[] str_;
  26.             str_ = new char[capacity_];
  27.             strcpy(str_, other.str_);
  28.         }
  29.         return *this;
  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) const {
  56.         if (index < 0 || index >= size_) {
  57.             cout << "Некорректный индекс" << endl;
  58.         }
  59.         return str_[index];
  60.     }
  61.  
  62.     int size() const {
  63.         return size_;
  64.     }
  65.  
  66.     int capacity() const {
  67.         return capacity_;
  68.     }
  69.  
  70.     ~String() {
  71.         delete[] str_;
  72.     }
  73.  
  74.  
  75. private:
  76.     int size_;
  77.     int capacity_;
  78.     char* str_;
  79. };
  80.  
  81.  
  82. int main() {
  83.     setlocale(LC_ALL, "ru");
  84.  
  85.     String str1;
  86.  
  87.     String str2(30);
  88.  
  89.     String str3("Привет");
  90.  
  91.     cout << str3.get_string() << endl;
  92.  
  93.     str3.set_string("Новая строка");
  94.  
  95.     cout << str3.get_string() << endl;
  96.  
  97.     str1.set_string("Еще одна новая строка");
  98.  
  99.     cout << str1.get_string() << endl;
  100.  
  101.     String str4(str3);
  102.  
  103.     cout << str4.get_string() << endl;
  104.    
  105.     str4 = str1;
  106.  
  107.     cout << str1.get_string() << endl;
  108.  
  109.     cout << str4.get_string() << endl;
  110.  
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment