Добавил:
Рад, если кому-то помог Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
0
Добавлен:
01.11.2025
Размер:
1.66 Кб
Скачать
#include <iostream>
#include <locale>
using namespace std;

class Door {
    int height;
    int width;
public:
    Door(int h, int w) : height(h), width(w) {}
    
    // а) Только функция-член класса
    Door& operator=(const Door& other) {
        height = other.height;
        width = other.width;
        return *this;
    }
    
    // б) Только функция не член класса
    friend bool operator==(const Door& d1, const Door& d2);
    
    // с) Любой способ
    Door operator+(const Door& other) const {
        return Door(height + other.height, width + other.width);
    }
    
    // с) Альтернативный вариант как функция не член
    friend Door operator-(const Door& d1, const Door& d2);
    
    void print() const {
        cout << "Высота: " << height << ", Ширина: " << width << endl;
    }
};

// б) Функция не член класса
bool operator==(const Door& d1, const Door& d2) {
    return d1.height == d2.height && d1.width == d2.width;
}

// с) Функция не член класса
Door operator-(const Door& d1, const Door& d2) {
    return Door(d1.height - d2.height, d1.width - d2.width);
}

int main() {
    setlocale(LC_ALL, "ru_RU.UTF-8");
    
    Door d1(200, 80);
    Door d2(210, 85);
    
    cout << "Дверь 1: ";
    d1.print();
    cout << "Дверь 2: ";
    d2.print();
    
    Door d3 = d1 + d2;
    cout << "Сумма: ";
    d3.print();
    
    cout << "Двери равны: " << (d1 == d2) << endl;
    
    return 0;
}
Соседние файлы в папке Лаба4