Advertisement
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
- {
- private:
- char *str;
- int length;
- public:
- STRING()
- {
- cout << "По умолчанию " << endl;
- length = 0;
- str = new char[1];
- str[0] = '\0';
- }
- ~STRING()
- {
- cout << "Вызван деструктор " << this << endl;
- delete[] this->str;
- }
- STRING(char *str)
- {
- cout << "Вызван конструктор с параметром char* " << this << 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 = 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';
- }
- 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;
- newStr.length = this->length + other.length;
- newStr.str = new char[newStr.length + 1];
- int i = 0;
- for (; i < this->length; i++)
- {
- newStr.str[i] = this->str[i];
- }
- for (int j = 0; j < other.length; j++, i++)
- {
- newStr.str[i] = other.str[j];
- }
- newStr.str[newStr.length] = '\0';
- return newStr;
- };
- STRING& operator +=(const STRING &other)
- {
- cout << "Вызван оператор += " << endl;
- STRING newStr;
- newStr.length = this->length + other.length;
- newStr.str = new char[newStr.length + 1];
- int i = 0;
- for (; i<this->length; i++)
- {
- newStr.str[i] = this->str[i];
- }
- for (int j = 0; j< other.length; j++, i++)
- {
- newStr.str[i] = other.str[j];
- ;
- }
- newStr.str[newStr.length] = '\0';
- delete[] str;
- length = newStr.length;
- 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;
- }
- char& operator [](int index)
- {
- return this->str[index];
- }
- void Print()
- {
- cout << str;
- cout << endl;
- }
- };
- int main()
- {
- setlocale(LC_ALL, "rus");
- STRING a("Pupa");
- STRING b("Lupa");
- STRING c(b);
- cout << c << endl;
- STRING result;
- cout << "============================================================" << endl << endl;
- result = a + b;
- cout << result << endl;
- cout << "============================================================" << endl << endl;
- a = b;
- cout << a << endl;
- cout << "============================================================" << endl << endl;
- c += a;
- cout << c << endl;
- cout << "============================================================" << endl << endl;
- cout << a[0] << endl;
- cout << a << endl;
- a[0] = 'P';
- cout << a << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement