Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication2.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include<iostream>
- using namespace std;
- class STRING
- {
- private:
- char *str;
- int length;
- public:
- STRING()
- {
- cout << "По умолчанию " << endl;
- length = 0;
- str = new char[1];
- str[0] = '\0';
- }
- ~STRING()
- {
- cout << "Деструктор " << endl;
- delete[] this->str;
- }
- STRING(char *str)
- {
- cout << "С параметром " << endl;
- length = strlen(str);
- this->str = new char[length + 1];
- for (int i = 0; i < length; i++)
- {
- this->str[i] = str[i];
- }
- this->str[length] = '\0';
- }
- STRING(const STRING &other)
- {
- cout << "Копирование " << endl;
- length = strlen(other.str);
- this->str = new char[length + 1];
- for (int i = 0; i < length; i++)
- {
- this->str[i] = other.str[i];
- }
- this->str[length] = '\0';
- }
- STRING& operator =(const STRING &other)
- {
- cout << "Присваивание " << endl;
- if (this->str != '\0')
- {
- delete[] str;
- }
- length = other.length;
- this->str = new char[length + 1];
- for (int i = 0; i < length; i++)
- {
- this->str[i] = other.str[i];
- }
- this->str[length] = '\0';
- return *this;
- }
- operator const char*() const
- {
- cout << "Приведение типа " << endl;
- return str;
- }
- STRING operator +(const STRING &other)
- {
- cout << "Оператор + " << endl;
- STRING newStr;
- int thisLength = strlen(this->str);
- int otherLength = strlen(other.str);
- newStr.length = thisLength + otherLength;
- newStr.str = new char[newStr.length + 1];
- int i = 0;
- for (; i < thisLength; i++)
- {
- newStr.str[i] = this->str[i];
- }
- for (int j = 0; j < otherLength; j++, i++)
- {
- newStr.str[i] = other.str[j];
- }
- newStr.str[newStr.length] = '\0';
- return newStr;
- };
- STRING& operator +=(const STRING &other)
- {
- cout << "Оператор += " << endl;
- STRING newStr;
- int thisLength = strlen(this->str);
- int otherLength = strlen(other.str);
- newStr.length = thisLength + otherLength;
- newStr.str = new char[newStr.length + 1];
- int i = 0;
- for (; i<thisLength; i++)
- {
- newStr.str[i] = this->str[i];
- }
- for (int j = 0; j< otherLength; j++, i++)
- {
- newStr.str[i] = other.str[j];
- ;
- }
- newStr.str[newStr.length] = '\0';
- delete[] str;
- length = strlen(newStr.str);
- this->str = new char[length + 1];
- for (int i = 0; i < length; i++)
- {
- this->str[i] = newStr.str[i];
- }
- this->str[length] = '\0';
- return *this;
- }
- void Print()
- {
- cout << str;
- cout << endl;
- }
- };
- int main()
- {
- setlocale(LC_ALL, "rus");
- STRING test;
- cout << test;
- STRING a("123");
- cout << a << endl;
- STRING b("456");
- STRING c(b);
- cout << c << endl;
- cout << "============================================================" << endl << endl;
- STRING result;
- result = a + b;
- cout << result << endl;
- cout << "============================================================" << endl << endl;
- a = b;
- cout << a << endl;
- cout << "============================================================" << endl;
- c += a;
- cout << c << endl;
- cout << "============================================================" << endl << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement