|
|
If the Edge is used in its generic version, the following constructors are defined. Note that Edge is the only instance in the Graph classes of an object needing a constructor with parameters, in this case, the source and destination Vertices defining the Edge:
Edge(Vertex* source, Vertex* destination); Edge(const Edge& e);
which allows for Graph g, Vertices v1 and v2, and Edges e1 and e2 declarations such as:
Edge e1(&v1, &v2); Edge e2(e1); g.insert(new Edge(&v1, &v2));
When a new Edge is created from an existing one, the new Edge and the existing one both share the same source and destination Vertices. When an Edge is destroyed, it is removed from any Graphs that still contain it, and from the Edge sets of its source and destination Vertices.
Often, assigning a variable name to an Edge is unnecessary since (1) the Edge gains identity from the two Vertices which it connects, and (2) it usually is accessed while one of these two Vertices is being processed by an algorithm. This is another instance of where using the dynamic form of declaration is preferable, rather than creating new variable names for a substantial number of Edges.
Thus, in our example, we can create Transport_Time edges in the following way:
. . . widget.insert(new Transport_Time(mod("A"),mod("D"))); widget.insert(new Transport_Time(mod("A"),mod("F"))); etc. . . .
When an Edge is inserted into a Graph, the Graph package will automatically insert the Vertices that are being used as the source and destination of the Edge. This can in some instances save a substantial amount of coding time.
Using this information, we can shorten the code for the Module and Transport_Time inserts in our example to the following:
. . . widget.insert( new Transport_Time( new Module("A"), new Module("D"))); widget.insert( new Transport_Time( mod("A"), new Module("F"))); etc. . . .
We have used this shortcut in the complete example program in the appendix to this tutorial.
If a user Edge must be defined for the Graph, then the Edge constructors are not automatically defined. Like the constructors for the Graph class, they must be created in the user definition:
class Myedge: public Edge { public: . . . Myedge(Myvertex* v1, Myvertex* v2) : Edge(v1, v2) {...}; Myedge(const Myedge& e) : Edge(e) {...}; . . . };
Again, the two-parameter Edge constructor can be enhanced with user data that needs to be initialized, such as we did in the section, ``Using the Graph Classes to Create Objects'', in the definition of the user-derived Edge, Transport_Time.