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



#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.empty()) {
            name = "Unknown";
        } else {
            name = n;
        }
    }

    // Проверка типа животного
    void setType(string t) {
        if (t.empty()) {
            type = "Unknown";
        } else {
            type = t;
        }
    }

    // Проверка веса
    void setWeight(int w) {
        if (w > 0) {
            weight = w;
        } else {
            weight = 1;
        }
    }

    string getName() {
        return name;
    }

    string getType() {
        return type;
    }

    int getWeight() {
        return weight;
    }

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

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

    // Сравнение по весу
    bool operator>(Animal other) {
        return weight > other.weight;
    }

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

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

    // Вывод информации
    void print() {
        cout << "Name: " << name << endl;
        cout << "Type: " << type << endl;
        cout << "Weight: " << weight << " kg" << endl;
    }
};

int main() {
    Animal animal1("Barsik", "Cat", 10);
    Animal animal2("Sharik", "Dog", 20);

    cout << "First animal:" << endl;
    animal1.print();

    cout << endl << "Second animal:" << endl;
    animal2.print();

    cout << endl << "Increase weight of the first animal:" << endl;
    ++animal1;
    animal1.print();

    cout << endl << "Decrease weight of the second animal:" << endl;
    --animal2;
    animal2.print();

    cout << endl << "Comparison:" << endl;

    if (animal1 > animal2) {
        cout << animal1.getName() << " is heavier." << endl;
    } else if (animal1 < animal2) {
        cout << animal2.getName() << " is heavier." << endl;
    } else {
        cout << "Animals have the same weight." << endl;
    }

    if (animal1 == animal2) {
        cout << "Weights are equal." << endl;
    } else {
        cout << "Weights are different." << endl;
    }

    return 0;
}