|
|
Having declared our user types, we now want to be able to easily create, insert, and remove Modules and Transport_Times from the Product. Once the user types have been created as just described, then we can statically declare the objects that are represented in Figure 1:
Product widget; Module m0("A"); Module m1("D"); Module m2("F"); Module m3("C"); Module m4("J"); Module m5("M"); Module m6("B"); Module m7("K"); Module m8("Z"); Transport_Time t0(&m0, &m1, 5); Transport_Time t1(&m0, &m2, 1); Transport_Time t2(&m1, &m3, 2); Transport_Time t3(&m1, &m4, 0); Transport_Time t4(&m5, &m4, 0); Transport_Time t5(&m3, &m6, 0); Transport_Time t6(&m4, &m7, 6); Transport_Time t7(&m4, &m6, 7); Transport_Time t8(&m7, &m8, 8); Transport_Time t9(&m6, &m8, 9);
Note that we have here attached variable names to the newly-created Modules and Transport_Times: if many such declarations are to be made and ultimately used, these individual names are awkward at best. We'll see in the sections, ``Vertex Constructors and Destructors'', and ``Edge Constructors and Destructors'', that there are ways to avoid this problem: however, for simplicity we will continue with these for now.
To insert and remove Modules and Transport_Times from the Product, simply
. . . widget.insert(&m0); widget.insert(&t0); etc. . . . widget.remove(&m0); widget.remove(&t0); etc. . . .
using member functions of the Graph class from which Product widget is derived. Note that insert and remove take pointer arguments, as we described in the section, ``What is a Graph?''.
The destruction of these statically declared objects happens automatically when these objects go out of scope, as with any other object type in C++. More information on Graph class object destruction can be found in the sections, ``Graph Constructors and Destructors'', ``Vertex Constructors and Destructors'', and ``Edge Constructors and Destructors''.