The C++ Graph Classes: A Tutorial - Graph(C++) and Graph_alg(C++)

Graph Constructors and Destructors

If the generic version of Graph is sufficient for the programming problem, then the following constructors are defined:

       Graph(const Graph&);

which allows declarations of the following kind:

       Graph g;
       Graph g(g2); // g2 is a previously declared Graph
       Graph g = new Graph();

(Of course, whenever you dynamically create objects, you should destroy them properly when they are no longer needed. The destructor for Graph will in this case be invoked when you invoke the delete operator that is supplied with all C++ classes.)

When a new Graph is created from an existing one, the new Graph and the existing one both include the same Vertices and Edges. When a Graph is destroyed, the Vertices and Edges that still exist in the Graph are removed from the Graph. The Vertex and Edge objects, however, may still be used to form new Graphs, etc.

If a user Graph must be defined to include user data for the Graph, then these constructors are not automatically defined. They must be created in the user Graph definition:

       class Mygraph: public Graph {
           Mygraph() : Graph() {...};
           Mygraph(const Mygraph& g) : Graph(g) {...};

Note that, as usual, the parameterless 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 Module.

Next topic: Graph Operators
Previous topic: The Graph Classes

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005