Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <cstring>
- using namespace std;
- class String {
- public:
- void SetString(const char new_str[]) {
- delete[] str;
- int new_capacity = strlen(new_str) + 1;
- capacity = new_capacity;
- str = new char[new_capacity];
- strcpy(str, new_str);
- size = new_capacity - 1;
- }
- private:
- int size;
- int capacity;
- char* str;
- };
- int main() {
- // для проверки:
- String my_string;
- cout << "Size: " << my_string.Size() << " Capacity: " << my_string.Capacity() << endl;
- my_string.SetString("Hello");
- cout << my_string.GetString() << endl;
- cout << "Size: " << my_string.Size() << " Capacity: " << my_string.Capacity() << endl;
- my_string.SetString("Hello my dear friend");
- cout << my_string.GetString() << endl;
- cout << "Size: " << my_string.Size() << " Capacity: " << my_string.Capacity() << endl;
- my_string.SetChar(2, 'm');
- cout << my_string.GetString() << endl;
- cout << my_string.GetChar(1) << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement