Associations and Links
Assume A and B are sets:
B = { b1, b2, b3 }
Note: This doesn't quite work, because it doesn't say if R is a subset of A x B or of B x A. If we attach roles, then we can make this distinction:
An object diagram looks like this:
The connecting lines are called links. These will probably be represented by pointers in C++.
If we further assume the relationship is bi-directional with a 1 to 2 multiplicity, then here is how we might represent it in C++:
class B
{
A* first;
// etc.
};
If the links are important to our application, then we might want to represent them as objects instead of pointers. Link objects are instances of association classes.
Note: Mathematicians don't distinguish between pairs and elements nor between relationships and sets. The pairs (a1, b1), (a1, b2), etc. are viewed as mathematical objects, just as the elements a1, b1, b2, etc, and the relationship R is viewed as a mathematical set just as A and B.
The UML notation for an association class is:
Now links are link objects:
Power Types (Meta-Level Classes)
Assume U is the universe of all objects:
The power set of U, P(U) is the set of all subsets of U:
Subsets of P(U) are called power sets. For example:
BBB = all subsets of U containing b3.
We can also introduce P(U) as a meta-level class. Every base-level class points to the object in P(U) that represents it:
The association in the diagram indicate that it is possible to ask instances of A and B what their types are.
class U
{
protected:
P(U)* type;
// etc.
};
class A: public U { ... };
class B: public U { ... };