Advertisement
35657

Untitled

Feb 15th, 2024
864
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <iostream>
  2.  
  3.  
  4. using namespace std;
  5.  
  6.  
  7. class String {
  8.  
  9. public:
  10.  
  11.     String() {
  12.         size_ = 0;
  13.         capacity_ = 15;
  14.         str_ = new char[capacity_];
  15.     }
  16.  
  17.     String(const int string_capacity) {
  18.         size_ = 0;
  19.         capacity_ = string_capacity;
  20.         str_ = new char[capacity_];
  21.     }
  22.  
  23.     String(const char* new_string) {
  24.         capacity_ = strlen(new_string) + 1;
  25.         str_ = new char[capacity_];
  26.         strcpy(str_, new_string);
  27.         size_ = capacity_ - 1;
  28.     }
  29.  
  30.     void SetString(const char* new_string) {
  31.         int new_space = strlen(new_string) + 1;
  32.         if (capacity_ < new_space) {
  33.             delete[] str_;
  34.             capacity_ = new_space;
  35.             str_ = new char[capacity_];
  36.         }
  37.         strcpy(str_, new_string);
  38.         size_ = capacity_ - 1;
  39.     }
  40.  
  41.     const char* GetString() {
  42.         return str_;
  43.     }
  44.  
  45.  
  46. private:
  47.     char* str_;
  48.     int size_;
  49.     int capacity_;
  50. };
  51.  
  52.  
  53. int main() {
  54.     setlocale(LC_ALL, "ru");
  55.  
  56.     String str1;
  57.  
  58.     String str2(30);
  59.  
  60.     String str3("Hello");
  61.  
  62.     cout << str3.GetString() << endl;
  63.  
  64.     str3.SetString("New_str3");
  65.  
  66.     cout << str3.GetString() << endl;
  67.  
  68.     str1.SetString("New_str1");
  69.  
  70.     cout << str1.GetString() << endl;
  71.  
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement