Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- using namespace std;
- class String {
- public:
- String() : size_(0), capacity_(15), str_(new char[capacity_]) {}
- explicit String(const int string_capacity) : size_(0), capacity_(string_capacity), str_(new char[capacity_]) {}
- String(const char* new_string) : size_(strlen(new_string)), capacity_(size_ + 1), str_(new char[capacity_]) {
- strcpy(str_, new_string);}
- String(const String& other) : size_(other.size_), capacity_(other.capacity_), str_(new char[capacity_]) {
- strcpy(str_, other.str_);}
- String& operator=(const String& other) {
- if (&other != this) {
- size_ = other.size_;
- capacity_ = other.capacity_;
- delete[] str_;
- str_ = new char[capacity_];
- strcpy(str_, other.str_);
- }
- return *this;
- }
- void set_string(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* get_string() {
- return str_;
- }
- void set_char(const int index, const char ch) {
- if (index < 0 || index >= size_) {
- cout << "Некорректный индекс" << endl;
- return;
- }
- str_[index] = ch;
- }
- char get_char(const int index) const {
- if (index < 0 || index >= size_) {
- cout << "Некорректный индекс" << endl;
- }
- return str_[index];
- }
- int size() const {
- return size_;
- }
- int capacity() const {
- return capacity_;
- }
- ~String() {
- delete[] str_;
- }
- private:
- int size_;
- int capacity_;
- char* str_;
- };
- int main() {
- setlocale(LC_ALL, "ru");
- String str1;
- String str2(30);
- String str3("Привет");
- cout << str3.get_string() << endl;
- str3.set_string("Новая строка");
- cout << str3.get_string() << endl;
- str1.set_string("Еще одна новая строка");
- cout << str1.get_string() << endl;
- String str4(str3);
- cout << str4.get_string() << endl;
- str4 = str1;
- cout << str1.get_string() << endl;
- cout << str4.get_string() << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment