Last active
April 6, 2021 14:52
-
-
Save grayed/840900bd5fde4f911779bce0a7c623b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdafx.h" | |
#include <iostream> | |
#include <string> | |
#include <set> | |
class person; | |
class car | |
{ | |
private: | |
std::string model, vin; | |
std::set<person *> owners; | |
public: | |
void setModel(std::string model_) | |
{ model = model_; } | |
std::string getModel() const | |
{ return model; } | |
void setVin(std::string vin_) | |
{ vin = vin_; } | |
std::string getVin() const | |
{ return vin; } | |
car(std::string vin, std::string model, person *owner_) | |
{ | |
this->vin = vin; | |
this->model = model; | |
owners.insert(owner_); | |
} | |
void addOwner(person *newOwner); | |
void removeOwner(person *Name) | |
{ owners.erase(Name); } | |
size_t getOwnersCount() const | |
{ return owners.size(); } | |
}; | |
class person { | |
std::string name; | |
std::set<car *> cars; | |
void actualAddCar(car &car) { /* изменить cars */ } | |
friend class car; | |
public: | |
person(const std::string &name) { /* ... */ } | |
std::string getName() const | |
{ return name; } | |
void addCar(car &car) | |
{ /* вызывать actualAddCar(); вызвать car.actualAddOwner(); */ } | |
void removeCar(car &car) | |
{ /* ... */ } | |
size_t getCarCount() const | |
{ return cars.size(); } | |
}; | |
void car::addOwner(person *newOwner) | |
{ newOwner->actualAddCar(*this); /* ... */ } | |
int main(int argc, char* argv[]) | |
{ | |
person* persons[] = { | |
new person("Ivan Ivanov"), | |
new person("Ivanka Ivanova") | |
}; | |
car* cars[] = { | |
new car("123456", "Zaporozhec", persons[0]), | |
new car("234567", "Oka", persons[1]), | |
new car("345678", "Smart", persons[1]) | |
}; | |
for (size_t i = 0; i < sizeof(persons)/sizeof(persons[0]); i++) | |
std::cout << persons[i]->getName() << " has " << persons[i]->getCarCount() << " cars" << std::endl; | |
// https://gist.github.com/grayed | |
// Ожидаемый вывод: | |
// Ivan Ivanov has 1 cars | |
// Ivanka Ivanova has 2 cars | |
// (1) доделать | |
// (2) добавить контроль входных данных в конструкторах и методах (кидать исключения) | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment