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