|
|
|
|
|
|
|
Date( /* in */ int initMo,
/* in */ int initDay,
/* in */ int initYr,
/* in */ const char* msgStr );
// Constructor
// Precondition:
// 1 <= initMo <= 12
// &&Sc 1 <= initDay <= maximum number of days in initMo
// && 1582 < initYr
// && msgStr is assigned
// Postcondition:
// New class object is constructed with a date of
// initMo, initDay, initYr and a message string msgStr
Date( const Date& otherDate );
// Copy-constructor
// Postcondition:
// New class object is constructed with date and
// message string the same as otherDate's
// Note:
// This constructor is implicitly invoked whenever a
// Date object is passed by value, is returned as a
// function value, or is initialized by another
// Date object in a declaration
~Date();
// Destructor
// Postcondition:
// Message string is destroyed
private:
int mo;
int day;
int yr;
char* msg;
}; |
|
|
|
|
|
|
|
|
The Date class of Figure 17-10 provides a destructor function named ~Date. A class destructor, identified by a tilde ( ~ ) preceding the name of the class, can be thought of as the opposite of a constructor. Just as a constructor is implicitly invoked when control reaches the declaration of a class object, a destructor is implicitly invoked when the class object is destroyed. A class object is destroyed when it goes out of scope. (An automatic object goes out of scope when control leaves the block in which it is declared. A static object goes out of scope when program execution terminates.) The following blockwhich might be a function body, for exampleincludes remarks at the locations where the constructor and destructor are invoked: |
|
|
|
|
|