Загрузка данных



#include <iostream>
#include <string>
using namespace std;

class Animal {
private:
    string name;
    string type;
    int weight;

public:
    Animal(string n, string t, int w) {
        setName(n);
        setType(t);
        setWeight(w);
    }

    void setName(string n) {
        if (n != "") {
            name = n;
        }
        else {
            name = "Без имени";
        }
    }

    void setType(string t) {
        if (t != "") {
            type = t;
        }
        else {
            type = "Неизвестно";
        }
    }

    void setWeight(int w) {
        if (w > 0) {
            weight = w;
        }
        else {
            weight = 1;
        }
    }

    string getName() {
        return name;
    }

    int getWeight() {
        return weight;
    }

    // Увеличиваем вес на 5
    Animal& operator++() {
        weight = weight + 5;
        return *this;
    }

    // Уменьшаем вес на 5
    Animal& operator--() {
        if (weight > 5) {
            weight = weight - 5;
        }
        else {
            weight = 1;
        }

        return *this;
    }

    // Сравниваем вес
    bool operator>(Animal a) {
        return weight > a.weight;
    }

    bool operator<(Animal a) {
        return weight < a.weight;
    }

    bool operator==(Animal a) {
        return weight == a.weight;
    }

    void show() {
        cout << "Имя: " << name << endl;
        cout << "Вид: " << type << endl;
        cout << "Вес: " << weight << " кг" << endl;
    }
};

int main() {
    setlocale(LC_ALL, "Russian");

    Animal a1("Барсик", "Кот", 10);
    Animal a2("Шарик", "Собака", 20);

    cout << "Первое животное:" << endl;
    a1.show();

    cout << endl << "Второе животное:" << endl;
    a2.show();

    cout << endl << "Увеличиваем вес первого животного:" << endl;
    ++a1;
    a1.show();

    cout << endl << "Уменьшаем вес второго животного:" << endl;
    --a2;
    a2.show();

    cout << endl << "Сравнение животных:" << endl;

    if (a1 > a2) {
        cout << "Первое животное тяжелее" << endl;
    }
    else if (a1 < a2) {
        cout << "Второе животное тяжелее" << endl;
    }
    else {
        cout << "Вес животных одинаковый" << endl;
    }

    if (a1 == a2) {
        cout << "Веса равны" << endl;
    }
    else {
        cout << "Веса не равны" << endl;
    }

    return 0;
}