|
|
|
|
|
|
|
{ Date conf(1, 30, 1998, Conference);¼ Constructor is invoked here . . .
}¼ Destructor is invoked here because conf goes out of scope } |
|
|
|
|
|
|
|
|
In the implementation file date.cpp, the implementation of the class destructor is very simple: |
|
|
|
|
|
|
|
|
Date::~Date()
// Destructor
// Postcondition:
// Array pointed to by msg is no longer on the free store
{
delete [] msg;
} |
|
|
|
|
|
|
|
|
You cannot pass parameters to a destructor and, as with a class constructor, you must not declare the data type of the function. |
|
|
|
|
|
|
|
|
Until now, we have not needed class destructors. In all previous examples of classes, the private data have been enclosed entirely within the abstraction barrier of the class. For example, in Chapter 15, a TimeType class object encapsulates all of its data: |
|
|
|
|
|
|
|
|
When startTime goes out of scope, destruction of startTime implies destruction of all of its component data. |
|
|
|
|
|
|
|
|
With the Date class, some of the data are enclosed within the abstraction barrier and some of the data are not (Figure 17-9). Without the destructor function ~Date, destruction of a class object would deallocate pointer to the dynamic array, but would not deallocate the array itself. The result would be a memory leak; the dynamic array would remain allocated but no longer accessible. |
|
|
|
|
|