Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "MyString.h"
- // конструкторы, деструктор
- MyString::MyString()
- {
- str = nullptr;
- length = 0;
- }
- MyString::MyString(const char* other)
- {
- length = strlen(other);
- this->str = new char[length + 1];
- for (int i = 0; i < length; i++)
- {
- this->str[i] = other[i];
- }
- this->str[length] = '\0';
- }
- MyString::MyString(const MyString& other)
- {
- 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';
- }
- MyString::MyString(MyString&& other)
- {
- this->length = other.length;
- this->str = other.str;
- other.str = nullptr;
- }
- MyString::~MyString()
- {
- delete[] this->str;
- }
- // перегрузка присваивания и конкатенации
- //MyString& MyString::operator= (const MyString& other)
- //{
- // if (this->str != nullptr)
- // {
- // delete[] str;
- // }
- //
- // 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';
- //
- // return *this;
- //}
- MyString& MyString::operator=(const MyString& other)
- {
- if (length != other.length) {
- length = other.length;
- delete[] str;
- str = new char[length + 1];
- }
- strcpy_s(str, length + 1, other.str);
- return *this;
- }
- MyString MyString::operator+ (const MyString& other)
- {
- MyString newStr;
- int thisLength = strlen(this->str);
- int otherLength = strlen(other.str);
- newStr.length = thisLength + otherLength;
- newStr.str = new char[thisLength + otherLength + 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[thisLength + otherLength] = '\0';
- return newStr;
- }
- //MyString& MyString::operator+= (const char& symbol)
- //{
- // this->str[length] = symbol;
- // ++length;
- // str[length] = '\0';
- // return *this;
- //}
- // перегрузка операторов сравнения
- bool MyString::operator== (const MyString& other)
- {
- if (this->length != other.length)
- {
- return false;
- }
- for (int i = 0; i < this->length; i++)
- {
- if (this->str[i] != other.str[i])
- {
- return false;
- }
- }
- return true;
- }
- bool MyString::operator!= (const MyString& other)
- {
- return !(this->operator== (other));
- }
- bool MyString::operator> (const MyString& other)
- {
- return (this->length > other.length);
- }
- bool MyString::operator< (const MyString& other)
- {
- return (this->length < other.length);
- }
- bool MyString::operator>= (const MyString& other)
- {
- return !(this->operator< (other));
- }
- bool MyString::operator<= (const MyString& other)
- {
- return !(this->operator> (other));
- }
- // перегрузка оператора индексирования
- char& MyString::operator[] (int index)
- {
- return this->str[index];
- }
- // перегрузка ввода/вывода, как дружественные функции
- ostream& operator<< (ostream& out, const MyString& other)
- {
- out << other.str;
- return out;
- }
- istream& operator>> (istream& in, MyString& other)
- {
- char BUFF[2048];
- in.getline(BUFF, sizeof BUFF);
- other = BUFF;
- return in;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement