
Вопрос задан 18.10.2023 в 01:51.
Предмет Информатика.
Спрашивает Геленджик Маняня.
Створіть програму, що імітує багатоквартирний будинок. Необхідно створити класи «Людина»,
«Квартира», «Дім». Клас «Квартира» містить динамічний масив об’єктів класу «Людина». Клас «Дім» містить масив об’єктів класу «Квартира». Кожен з класів містить змінні-члени і функції-члени, які необхідні для предметної області класу. Не забувайте забезпечити класи різними конструкторами (конструктор копіювання обов’язковий), деструкторами. В main протестувати роботу отриманого набору класів. На мові С++

Ответы на вопрос

Отвечает Блем Данил.
#include
#include
#include
using namespace std;
class Person {
public:
Person(const string& name, int age) : name_(name), age_(age) {}
Person(const Person& other) : name_(other.name_), age_(other.age_) {}
string getName() const { return name_; }
int getAge() const { return age_; }
private:
string name_;
int age_;
};
class Apartment {
public:
Apartment(int number) : number_(number) {}
Apartment(const Apartment& other) : number_(other.number_), residents_(other.residents_) {}
void addResident(const Person& person) {
residents_.push_back(person);
}
void printResidents() const {
cout << "Residents of Apartment " << number_ << ":\n";
for (const auto& person : residents_) {
cout << "Name: " << person.getName() << ", Age: " << person.getAge() << endl;
}
cout << endl;
}
private:
int number_;
vector residents_;
};
class House {
public:
House(int numberOfApartments) : apartments_(numberOfApartments) {}
void addApartment(const Apartment& apartment) {
apartments_.push_back(apartment);
}
void printResidents() const {
cout << "Residents of the House:\n";
for (const auto& apartment : apartments_) {
apartment.printResidents();
}
}
private:
vector apartments_;
};
int main() {
// Create some people
Person person1("John", 25);
Person person2("Alice", 30);
Person person3("Bob", 35);
// Create some apartments and add residents
Apartment apartment1(101);
apartment1.addResident(person1);
apartment1.addResident(person2);
Apartment apartment2(102);
apartment2.addResident(person3);
// Create a house and add apartments
House house(2);
house.addApartment(apartment1);
house.addApartment(apartment2);
// Print the residents of the house
house.printResidents();
return 0;
}
#include
#include
using namespace std;
class Person {
public:
Person(const string& name, int age) : name_(name), age_(age) {}
Person(const Person& other) : name_(other.name_), age_(other.age_) {}
string getName() const { return name_; }
int getAge() const { return age_; }
private:
string name_;
int age_;
};
class Apartment {
public:
Apartment(int number) : number_(number) {}
Apartment(const Apartment& other) : number_(other.number_), residents_(other.residents_) {}
void addResident(const Person& person) {
residents_.push_back(person);
}
void printResidents() const {
cout << "Residents of Apartment " << number_ << ":\n";
for (const auto& person : residents_) {
cout << "Name: " << person.getName() << ", Age: " << person.getAge() << endl;
}
cout << endl;
}
private:
int number_;
vector residents_;
};
class House {
public:
House(int numberOfApartments) : apartments_(numberOfApartments) {}
void addApartment(const Apartment& apartment) {
apartments_.push_back(apartment);
}
void printResidents() const {
cout << "Residents of the House:\n";
for (const auto& apartment : apartments_) {
apartment.printResidents();
}
}
private:
vector apartments_;
};
int main() {
// Create some people
Person person1("John", 25);
Person person2("Alice", 30);
Person person3("Bob", 35);
// Create some apartments and add residents
Apartment apartment1(101);
apartment1.addResident(person1);
apartment1.addResident(person2);
Apartment apartment2(102);
apartment2.addResident(person3);
// Create a house and add apartments
House house(2);
house.addApartment(apartment1);
house.addApartment(apartment2);
// Print the residents of the house
house.printResidents();
return 0;
}



Отвечает нейросеть ChatGpt. Будьте внимательны, ответы нейросети могут содержать ошибочные или выдуманные данные.
Звучить як цікава задача! Ось приклад реалізації у вашій програмі на мові C++:
cpp#include <iostream>
#include <vector>
#include <string>
class Person {
public:
Person(std::string name, int age) : name(name), age(age) {}
Person(const Person& other) : name(other.name), age(other.age) {}
std::string getName() const {
return name;
}
int getAge() const {
return age;
}
private:
std::string name;
int age;
};
class Apartment {
public:
Apartment(int number) : number(number) {}
Apartment(const Apartment& other) : number(other.number) {}
void addResident(const Person& person) {
residents.push_back(person);
}
void displayResidents() const {
std::cout << "Residents of Apartment #" << number << ":\n";
for (const auto& person : residents) {
std::cout << "Name: " << person.getName() << ", Age: " << person.getAge() << std::endl;
}
}
private:
int number;
std::vector<Person> residents;
};
class House {
public:
House() {}
void addApartment(const Apartment& apartment) {
apartments.push_back(apartment);
}
void displayApartments() const {
std::cout << "List of Apartments in the House:\n";
for (const auto& apartment : apartments) {
apartment.displayResidents();
std::cout << "------------\n";
}
}
private:
std::vector<Apartment> apartments;
};
int main() {
Person person1("John", 30);
Person person2("Alice", 25);
Person person3("Bob", 35);
Apartment apartment1(101);
apartment1.addResident(person1);
apartment1.addResident(person2);
Apartment apartment2(202);
apartment2.addResident(person3);
House house;
house.addApartment(apartment1);
house.addApartment(apartment2);
house.displayApartments();
return 0;
}
У цій програмі є три класи: Person
для представлення людей, Apartment
для представлення квартир і House
для представлення будинку. В класі Person
є змінні-члени для зберігання імені та віку, а в класі Apartment
- для зберігання номера квартири та вектору мешканців. Клас House
містить вектор об'єктів класу Apartment
.
Функції-члени дозволяють додавати мешканців до квартир та квартири до будинку, а також виводити інформацію про мешканців і квартири в консоль. У функції main()
створюються об'єкти класів і викликаються їхні методи для тестування.


Похожие вопросы
Топ вопросов за вчера в категории Информатика
Последние заданные вопросы в категории Информатика
Предметы
-
Математика
-
Литература
-
Алгебра
-
Русский язык
-
Геометрия
-
Английский язык
-
Химия
-
Физика
-
Биология
-
Другие предметы
-
История
-
Обществознание
-
Окружающий мир
-
География
-
Українська мова
-
Информатика
-
Українська література
-
Қазақ тiлi
-
Экономика
-
Музыка
-
Право
-
Беларуская мова
-
Французский язык
-
Немецкий язык
-
МХК
-
ОБЖ
-
Психология
-
Физкультура и спорт
-
Астрономия
-
Кыргыз тили
-
Оʻzbek tili