Advertisement
35657

Untitled

May 19th, 2023
1,003
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <cstring>
  5.  
  6.  
  7. using namespace std;
  8.  
  9. class String {
  10.  
  11. public:
  12.  
  13.     void SetString(const char new_str[]) {
  14.         delete[] str;
  15.         int new_capacity = strlen(new_str) + 1;
  16.         capacity = new_capacity;
  17.         str = new char[new_capacity];
  18.         strcpy(str, new_str);
  19.         size = new_capacity - 1;
  20.     }
  21.  
  22.  
  23. private:
  24.     int size;
  25.     int capacity;
  26.     char* str;
  27. };
  28.  
  29.  
  30. int main() {
  31.     // для проверки:
  32.     String my_string;
  33.     cout << "Size: " << my_string.Size() << " Capacity: " << my_string.Capacity() << endl;
  34.     my_string.SetString("Hello");
  35.     cout << my_string.GetString() << endl;
  36.     cout << "Size: " << my_string.Size() << " Capacity: " << my_string.Capacity() << endl;
  37.     my_string.SetString("Hello my dear friend");
  38.     cout << my_string.GetString() << endl;
  39.     cout << "Size: " << my_string.Size() << " Capacity: " << my_string.Capacity() << endl;
  40.     my_string.SetChar(2, 'm');
  41.     cout << my_string.GetString() << endl;
  42.     cout << my_string.GetChar(1) << endl;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement