Good morning everyone,
I have been studying the one-to-one association, which I understand well, but when I have been looking at the many-to-many association, and there I get a little lost, especially with the set container. I am going to put the header file of a many-to-many association.
One person teaches several subjects.
A subject is taught by several people.
person class:
#ifndef persona_h_
#define persona_h_
#include <string>
#include <set>
using namespace std;
class Asignatura; //Declaración adelantada
class Persona
{
public:
Persona(string nom, string direc);
void mostrar() const;
void imparte(Asignatura& asignatura);
void mostrarAsignaturas() const;
private:
string nombre;
string direccion;
typedef set<Asignatura*> Asignaturas;
Asignaturas asignaturas;
};
#endif
Class Subject:
#ifndef asignatura_h_
#define asignatura_h_
#include <string>
#include <set>
using namespace std;
class Persona; //Declaración adelantada
class Asignatura
{
public:
Asignatura(string nom, string areaa);
void mostrar() const;
void impartida(Persona& persona);
void mostrarPersonas() const;
private:
string nombre;
string area;
typedef set<Persona*> Personas;
Personas personas;
};
#endif
Well, up to here, I understand that what is done is like a kind of set of pointers from a person to several subjects (several subjects will be a set of pointers and each pointer points to each subject that they teach), unlike a subject is delivered by multiple people.
What I don't quite understand is what does the lines:
typedef set<Persona*> Personas;
Personas personas;
typedef set<Asignatura*> Asignaturas;
Asignaturas asignaturas;
Could someone explain that to me in more detail or at least if I'm on the right track of how to do many-to-many relationships?
Thank you very much to all.
set
it is nothing more than a container of those that are available in the STL (the standard C++ template library).This container has two basic features (it has more, but to summarize):
For this case, I understand that what interests the person who has written that code is above all the first property. Since, presumably, it prevents you from doing certain checks. In the following example you can check these two features in operation. In case someone is too lazy to try the code, the result is
123
.On the other hand,
typedef
it is used to define an alias. In modern C++ this word can be replaced byusing
, which is more powerful (especially with templates) and with more readable results:Aliases are used to avoid repeating long, repetitive statements. By defining an alias you are not creating a new type:
Well, for a many-to-many relationship you necessarily need a double relationship, that is, the elements of group A need to link with several elements of group B and the elements of group B need to link with several elements of group A. One way The quickest way to create these relationships is through two containers in the elements of each group... just as it is proposed in this code. The problem with this solution is that it requires having all the lists perfectly synchronized. In other words, if a student signs up for a subject, the student's list of subjects and the list of students in the corresponding subject will have to be updated... if this process fails (and we must also take into account that a student could low), the result can be quite disappointing.
For this specific case, this many-to-many relationship could also be created by adding a container in a single group... for example in
Asignaturas
. To know the subjects in which a student is enrolled, you will have to go through the list of subjects and ask each one if the student is on their list:But this is not a solution that can be generalized... the best solution will depend on the specific requirements of each development