Нужно перегрузить оператор + Предложите варианты Вот мой код: #include<iostream> using namespace std; //#define CONSTRUCTORS_CHECK class String { int size; //Размер строки char* str; //Адрес строки в динамической памяти public: int get_size() const { return size; } char* get_str() const { return str; } // Constructors: String(int size = 80) { this->size = size; this->str = new char[size] {}; cout << "DefConstructor:\t" << this << endl; } String(const char str[]) { this->size = strlen(str) + 1; this->str = new char[size] {}; for (int i = 0; str[i]; i++)this->str[i] = str[i]; cout << "Constructor:\t" << this << endl; } String(const String& other) { this->size = other.size; this->str = new char[size] {}; for (int i = 0; i < size; i++)this->str[i] = other.str[i]; cout << "CopyConstructor:" << this << endl; } ~String() { delete[] this->str; cout << "Destructor:\t" << this << endl; } // Operators: String& operator=(const String& other) { if (this == &other)return *this; delete[] this->str; this->size = other.size; this->str = new char[size] {}; for (int i = 0; i < size; i++)this->str[i] = other.str[i]; cout << "CopyAssignment: " << this << endl; return *this; } void print() { cout << "size:\t" << size << endl; cout << "str:\t" << str << endl; } }; ostream& operator<<(ostream& os, const String& obj) { return os << obj.get_str(); } void main() { setlocale(LC_ALL, ""); #ifdef CONSTRUCTORS_CHECK cout << typeid(typeid("Hello").name()).name() << endl; String str0; //Default constructor str0.print(); String str1 = "Hello"; //Single argument constructor cout << str1 << endl; String str2 = str1; //Copy Constructor cout << str2 << endl; str0 = str1; //Copy assignment cout << str0 << endl; cout << "\n---------------------------------\n"; str2 = str2; cout << str2 << endl; cout << "\n---------------------------------\n"; #endif // CONSTRUCTORS_CHECK String str1 = "Hello"; String str2 = "World"; String str3 = str1 + str2; cout << str3 << endl; } Code #include<iostream> using namespace std; //#define CONSTRUCTORS_CHECK class String { int size; //Размер строки char* str; //Адрес строки в динамической памяти public: int get_size() const { return size; } char* get_str() const { return str; } // Constructors: String(int size = 80) { this->size = size; this->str = new char[size] {}; cout << "DefConstructor:\t" << this << endl; } String(const char str[]) { this->size = strlen(str) + 1; this->str = new char[size] {}; for (int i = 0; str[i]; i++)this->str[i] = str[i]; cout << "Constructor:\t" << this << endl; } String(const String& other) { this->size = other.size; this->str = new char[size] {}; for (int i = 0; i < size; i++)this->str[i] = other.str[i]; cout << "CopyConstructor:" << this << endl; } ~String() { delete[] this->str; cout << "Destructor:\t" << this << endl; } // Operators: String& operator=(const String& other) { if (this == &other)return *this; delete[] this->str; this->size = other.size; this->str = new char[size] {}; for (int i = 0; i < size; i++)this->str[i] = other.str[i]; cout << "CopyAssignment: " << this << endl; return *this; } void print() { cout << "size:\t" << size << endl; cout << "str:\t" << str << endl; } }; ostream& operator<<(ostream& os, const String& obj) { return os << obj.get_str(); } void main() { setlocale(LC_ALL, ""); #ifdef CONSTRUCTORS_CHECK cout << typeid(typeid("Hello").name()).name() << endl; String str0; //Default constructor str0.print(); String str1 = "Hello"; //Single argument constructor cout << str1 << endl; String str2 = str1; //Copy Constructor cout << str2 << endl; str0 = str1; //Copy assignment cout << str0 << endl; cout << "\n---------------------------------\n"; str2 = str2; cout << str2 << endl; cout << "\n---------------------------------\n"; #endif // CONSTRUCTORS_CHECK String str1 = "Hello"; String str2 = "World"; String str3 = str1 + str2; cout << str3 << endl; } Буду благодарен!!!
String operator+(const String other) { char temp[100]; int end_Left = strlen(str); //длина первой строки int end_Right = strlen(other.str); //длина второй строки int i; //индекс для третей и первой строки int j = 0; //индекс для второй строки for (i = 0; i < end_Left; i++) { temp = str; } i--; //после цикла i перемещается на лишнюю единицу for (;j < end_Right;j++) { temp[i++] = other.str[j]; } temp = '\0'; //добавляем конец в строку return String(temp); }
При вставке кода квадратные скобки удалились поэтому код не работал. Добавляем #include <string.h> String operator+(const String other) { char temp[100]; int end_Left = strlen(str); //длина первой строки int end_Right = strlen(other.str); //длина второй строки int i; //индекс для третей и первой строки int j = 0; //индекс для второй строки for (i = 0; i < end_Left; i++) { temp(i) = str(i); //круглые скобки нужно изменить на квадратные } i--; //после цикла i перемещается на лишнюю единицу for (;j < end_Right;j++) { temp[i++] = other.str[j]; } temp(i) = '\0'; //добавляем конец в строку(круглые скобки нужно изменить на квадратные) return String(temp); }