Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class String {
- public:
- String() {
- size_ = 0;
- capacity_ = 15;
- str_ = new char[capacity_];
- }
- String(const int string_capacity) {
- size_ = 0;
- capacity_ = string_capacity;
- str_ = new char[capacity_];
- }
- String(const char* new_string) {
- capacity_ = strlen(new_string) + 1;
- str_ = new char[capacity_];
- strcpy(str_, new_string);
- size_ = capacity_ - 1;
- }
- void SetString(const char* new_string) {
- int new_space = strlen(new_string) + 1;
- if (capacity_ < new_space) {
- delete[] str_;
- capacity_ = new_space;
- str_ = new char[capacity_];
- }
- strcpy(str_, new_string);
- size_ = capacity_ - 1;
- }
- const char* GetString() {
- return str_;
- }
- private:
- char* str_;
- int size_;
- int capacity_;
- };
- int main() {
- setlocale(LC_ALL, "ru");
- String str1;
- String str2(30);
- String str3("Hello");
- cout << str3.GetString() << endl;
- str3.SetString("New_str3");
- cout << str3.GetString() << endl;
- str1.SetString("New_str1");
- cout << str1.GetString() << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement