08.04.2020 11:57
kolo...@hotmail.com kirjutas:
> Hi,
> i need to solve the following problem:
> (All Code is Pseudo-Code)
>
> I want to define a list of Persons.
> virtual class Person
> {
> Person(char* name, char* vorname, date birthday);
> char* name;
> char* vorname*
> date birthday;
> static int counter;
> Person* Father;
> Person* Mother;
>
> }
There is no need to use char* for strings in C++, use std::string instead.
>
> Now i want to tell between Man an Women
>
> class Man : Public Person
> {
> Man()
> : Person(char*, char*, date);
> bool prop1;
> short prop2;
> short prop3;
> // List an event has to be added to(Man-Specific) //right place here?
> }
>
> class Woman : public Person
> {
> Woman(char*, char*, date)
> : Person(char*, char*, date);
> bool prop1;
> short prop2;
> short prop3;
> // List an events has to be added to (Woman-Specific) //right place here?
> }
>
> So far, so good. Now there is an event that has to go in a List:
>
> add_Event(Person* Person2, place, date);
>
> The question is, where to put that Method?
Much more important is where you hold the data about the event. If
directly inside Person, then you have to duplicate and synchronize it
everywhere (imagine a football match event with 10,000 persons attending).
I would make a separate Event class, containing description, date and
time, etc. Then I would add a Participate member function in Person:
class Event {
// ...
};
class Person {
// ...
std::deque<std::shared_ptr<Event>> events_;
public:
void Participate(std::shared_ptr<Event> event) {
events_.push_back(event);
}
};
Usage example:
Person Mary("Smith", "Mary", "01.01.1980");
Person Gordon("Brown", "Gordon", "12.03.1975");
auto event1 = std::make_shared<Event>("Cinema", "today(20:00)");
Mary.Participate(event1);
Gordon.Participate(event1);
Many other ways to build up such a program are possible, the choice
depends primarily on what tasks the program has to carry out.
>
> - in a method in Person?
>
> - in a method in another class (SLIST)?
>
> - as a friend-function of both Man/Woman
Avoid friend functions as much as possible.