Загрузка...

Перегрузить оператор +

Тема в разделе C/C++ создана пользователем Zheka2017 9 июл 2019. 272 просмотра

  1. Zheka2017
    Zheka2017 Автор темы 9 июл 2019 2 11 мар 2017
    Нужно перегрузить оператор +
    Предложите варианты
    Вот мой код:

    Код

    #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;
    }
    Буду благодарен!!!
     
  2. de9x
    de9x 9 июл 2019 Frontend developer 141 3 янв 2019
    1.5 + 1.7. перегрузил:finger_up:
     
  3. Zheka2017
    Zheka2017 Автор темы 9 июл 2019 2 11 мар 2017
    нада операто + перегрузить de9x,
     
  4. NamePool
    NamePool 5 авг 2019 0 5 авг 2019
    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);
    }
     
  5. adziri
    adziri 6 авг 2019 mental handicap 8 20 дек 2017
    Ты хоть бы прочекал то, что написал..
     
  6. NamePool
    NamePool 7 авг 2019 0 5 авг 2019
    При вставке кода квадратные скобки удалились поэтому код не работал.
    Добавляем #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);
    }
     
    7 авг 2019 Изменено
Загрузка...
Top