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


#ifndef ANIMAL_H
#define ANIMAL_H

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

class Animal
{
private:
    string name;
    string type;
    string color;
    int age;
    double weight;

public:
    Animal();
    Animal(string n, string t, string c, int a, double w);
    Animal(const Animal& other);
    ~Animal();

    void setName(string n);
    void setType(string t);
    void setColor(string c);
    void setAge(int a);
    void setWeight(double w);

    string getName();
    string getType();
    string getColor();
    int getAge();
    double getWeight();

    void eat(int days);
    void changeName(string newName);
    void show();
};

#endif

#include "Animal.h"

Animal::Animal()
{
    name = "";
    type = "";
    color = "";
    age = 0;
    weight = 0;
}

Animal::Animal(string n, string t, string c, int a, double w)
{
    name = "";
    type = "";
    color = "";
    age = 0;
    weight = 0;

    setName(n);
    setType(t);
    setColor(c);
    setAge(a);
    setWeight(w);
}

Animal::Animal(const Animal& other)
{
    name = other.name;
    type = other.type;
    color = other.color;
    age = other.age;
    weight = other.weight;
}

Animal::~Animal()
{
    cout << "Объект удален: " << name << endl;
}

void Animal::setName(string n)
{
    if (n != "")
        name = n;
    else
        cout << "Ошибка: имя не может быть пустым" << endl;
}

void Animal::setType(string t)
{
    if (t != "")
        type = t;
    else
        cout << "Ошибка: вид не может быть пустым" << endl;
}

void Animal::setColor(string c)
{
    if (c != "")
        color = c;
    else
        cout << "Ошибка: окрас не может быть пустым" << endl;
}

void Animal::setAge(int a)
{
    if (a >= 0)
        age = a;
    else
        cout << "Ошибка: возраст не может быть отрицательным" << endl;
}

void Animal::setWeight(double w)
{
    if (w > 0)
        weight = w;
    else
        cout << "Ошибка: вес должен быть больше нуля" << endl;
}

string Animal::getName()
{
    return name;
}

string Animal::getType()
{
    return type;
}

string Animal::getColor()
{
    return color;
}

int Animal::getAge()
{
    return age;
}

double Animal::getWeight()
{
    return weight;
}

void Animal::eat(int days)
{
    if (days >= 0)
        weight = weight + weight * 0.01 * days;
    else
        cout << "Ошибка: количество дней не может быть отрицательным" << endl;
}

void Animal::changeName(string newName)
{
    setName(newName);
}

void Animal::show()
{
    cout << "Имя: " << name << endl;
    cout << "Вид: " << type << endl;
    cout << "Окрас: " << color << endl;
    cout << "Возраст: " << age << endl;
    cout << "Вес: " << weight << endl;
    cout << endl;
}

#include <iostream>
#include "Animal.h"
using namespace std;

int main()
{
    Animal a1;
    a1.setName("Барсик");
    a1.setType("Кот");
    a1.setColor("Серый");
    a1.setAge(3);
    a1.setWeight(4.5);

    Animal a2("Шарик", "Собака", "Рыжий", 5, 12.0);

    Animal a3(a2);

    Animal* a4 = new Animal;
    a4->setName("Мурка");
    a4->setType("Кошка");
    a4->setColor("Белый");
    a4->setAge(2);
    a4->setWeight(3.5);

    Animal* a5 = new Animal("Бобик", "Собака", "Черный", 4, 10.0);

    a1.show();
    a2.show();
    a3.show();
    a4->show();
    a5->show();

    a1.eat(5);
    a1.changeName("Мурзик");
    a1.show();

    a2.setAge(-5);
    a2.setWeight(-10);
    a2.setName("");

    cout << "Имя: " << a2.getName() << endl;
    cout << "Возраст: " << a2.getAge() << endl;
    cout << "Вес: " << a2.getWeight() << endl;

    delete a4;
    delete a5;

    return 0;
}