[Univ of Cambridge] [Dept of Engineering]
next up previous contents
Next: assignment operator Up: More on Classes Previous: Redefining operators

A class definition example

The following example highlights some of the more subtle issues associated with Constructors and Destructors.

It's the job of Constructors to allocate the resources required when new objects are created. If these are created using new then the Destructor should free the resources to stop memory being wasted. For example, if we want to store information about criminals we might define an object as follows

class criminal {
 string name;
 char* fingerprint; 
 int fingerprint_size;
}

where fingerprint points to an image. The memory (if any) allocated for the image would be freed by the destructor.



 

Tim Love
2001-07-05