|
|
|
|
|
|
|
The informal specification given above would be unacceptable to the user of the ADT. The descriptions of the operations are too imprecise and ambiguous to be helpful to the user. |
|
|
|
|
|
|
|
|
The second stageimplementationrequires us to (1) choose a concrete data representation for a date, and (2) implement each of the operations as a C++ function definition. The result is a C++ implementation file containing these function definitions. |
|
|
|
|
|
|
|
|
Specification of the ADT: The domain of our ADT is the set of all dates after the year 1582 A.D. in the form of a month, a day, and a year. We restrict the year to be after 1582 A.D. in order to simplify the ADT operations (10 days were skipped in 1582 in switching from the Julian to the Gregorian calendar). |
|
|
|
|
|
|
|
|
To represent the DateType ADT as program code we use a C++ class named DateType. The ADT operations become public member functions of the class. Let's now specify the operations more carefully. |
|
|
|
|
|
|
|
|
Construct a new DateType instance: For this operation we use a C++ default constructor, initializing the date to January 1 of the year 1583. The client code can reset the date at any time using the Set the date operation. |
|
|
|
|
|
|
|
|
Set the date: The client must supply three parameters for this operation: month, day, and year. Although we haven't yet determined a concrete data representation for a date, we must decide what data types the client should use for these parameters. We choose integers, where the month must be in the range 1 through 12, the day must be in the range 1 through the maximum number of days in the month, and the year must be greater than 1582. Notice that these range restrictions will be the precondition for invoking this operation. |
|
|
|
|
|
|
|
|
Inspect the date's month, Inspect the date's day, Inspect the date's year: These three operations are all observer operations. They give the client access, indirectly, to the private data. In the DateType class, we represent these operations as value-returning member functions with the following prototypes. |
|
|
|
|
|
|
|
|
int Month();
int Day();
int Year(); |
|
|
|
|
|
|
|
|
Why do we need these observer operations? Why not simply let the data representation of the month, day, and year be public instead of private so that the client can access the values directly? The answer is that the client should be allowed to inspect but not modify these values. If the data were |
|
|
|
|
|