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


#include <iostream>
#include <vector>
#include <memory>

using namespace std;

class Apartment
{
private:
    double area;
    int rooms;

public:
    Apartment(double area, int rooms);
    ~Apartment();

    friend ostream& operator<<(ostream& out, const Apartment& apartment);
};

class Building
{
private:
    vector<unique_ptr<Apartment>> apartments;

public:
    Building();
    ~Building();

    void addApartment(double area, int rooms);
    void removeApartment(int index);
    void demolish();

    friend ostream& operator<<(ostream& out, const Building& building);
};

Apartment::Apartment(double area, int rooms) : area(area), rooms(rooms) { cout << "Квартира создана" << endl; }

Apartment::~Apartment() { cout << "Квартира удалена" << endl; }

ostream& operator<<(ostream& out, const Apartment& apartment)
{
    out << "Площадь: " << apartment.area
        << ", комнат: " << apartment.rooms;

    return out;
}

Building::Building() : apartments() { cout << "Постройка создана" << endl; }

Building::~Building() { cout << "Постройка удалена" << endl; }

void Building::addApartment(double area, int rooms)
{
    apartments.push_back(make_unique<Apartment>(area, rooms));
}

void Building::removeApartment(int index)
{
    if (index >= 0 && index < apartments.size())
    {
        apartments.erase(apartments.begin() + index);
        cout << "Квартира удалена из постройки" << endl;
    }
    else
    {
        cout << "Неверный индекс" << endl;
    }
}

void Building::demolish()
{
    apartments.clear();
    cout << "Постройка снесена" << endl;
}

ostream& operator<<(ostream& out, const Building& building)
{
    out << "\nКвартиры в постройке:" << endl;

    if (building.apartments.empty())
    {
        out << "Нет квартир" << endl;
    }
    else
    {
        for (int i = 0; i < building.apartments.size(); i++)
        {
            out << i + 1 << ". "
                << *building.apartments.at(i)
                << endl;
        }
    }

    return out;
}

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

    Building building;

    building.addApartment(45.5, 2);
    building.addApartment(62.3, 3);
    building.addApartment(80.0, 4);

    cout << building << endl;

    building.removeApartment(1);

    cout << building << endl;

    building.demolish();

    cout << building << endl;

    return 0;
}