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(String&& other) : size_(other.size_), capacity_(other.capacity_), str_(other.str_) {
- other.str_ = nullptr;
- other.size_ = 0;
- other.capacity_ = 0;
- }
- 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;
- }
- String& operator=(String&& other) {
- if (&other != this) {
- size_ = other.size_;
- capacity_ = other.capacity_;
- delete[] str_;
- str_ = other.str_;
- other.str_ = nullptr;
- other.size_ = 0;
- other.capacity_ = 0;
- }
- return *this;
- }
- String& operator=(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;
- return *this;
- }
- char& operator[](const int index) {
- if (index < 0 || index >= size_) {
- cout << "Некорректный индекс" << endl;
- }
- return str_[index];
- }
- const char& operator[](const int index) const {
- if (index < 0 || index >= size_) {
- cout << "Некорректный индекс" << endl;
- }
- return str_[index];
- }
- bool operator==(const String& other) const {
- return strcmp(str_, other.str_) == 0;
- }
- bool operator!=(const String& other) const {
- return strcmp(str_, other.str_) != 0;
- }
- int size() const {
- return size_;
- }
- int capacity() const {
- return capacity_;
- }
- ~String() {
- delete[] str_;
- }
- friend ostream& operator<<(ostream& output, const String& str);
- private:
- int size_;
- int capacity_;
- char* str_;
- };
- ostream& operator<<(ostream& output, const String& str) {
- output << str.str_;
- return output;
- }
- istream& operator>>(istream& input, String& str) {
- int capacity = 15;
- int i = 0;
- char* new_str = new char[capacity];
- char ch;
- while (ch = input.get()) {
- if (ch == ' ' || ch == '\n') {
- break;
- }
- if (i == capacity - 1) {
- char* temp = new char[capacity *= 2];
- strcpy(temp, new_str);
- delete[] new_str;
- new_str = temp;
- }
- new_str[i++] = ch;
- }
- new_str[i] = '\0';
- str = new_str;
- delete[] new_str;
- return input;
- }
- int main() {
- setlocale(LC_ALL, "ru");
- String str1;
- String str2(30);
- String str3("Привет");
- cout << str3 << endl;
- str3= "Новая строка";
- cout << str3 << endl;
- str1 = "Еще одна новая строка";
- cout << str1 << endl;
- String str4(str3);
- cout << str4 << endl;
- str4 = str1;
- cout << str1 << endl;
- cout << str4 << endl;
- str4[5] = 'н';
- cout << str4 << endl;
- cout << (str3 == str4) << endl;
- cout << (str3 != str4) << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment