< previous page page_869 next page >

Page 869
Problem-Solving Case Study Manipulating Dates
0869-01.gif
Dates are often necessary pieces of information. Both the address book and the machine shop examples in Chapter 14 had a date as part of the data. In fact, the machine shop example had two dates: the date of purchase and the date of last service. Each time we needed a date, we defined it again.
Often our processing of dates calls for us to compare two dates, print out a date, or determine the date a certain number of days in the future. Let's stop this duplication of effort and do the job once and for alllet's write the code to support dates as an abstract data type.
The format for this case study needs to be a little different. Because we are developing only one software componentan ADTand not a complete program, we omit the Input and Output sections. Instead, we include two sections entitled Specification of the ADT and Implementation of the ADT.
Problem: Design and implement an ADT to represent a date. Make the domain and operations general enough to be used in any program that needs to perform these operations on dates. The informal specification of the ADT is given below.
TYPE
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
DateType
DOMAIN
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Each DateType value is a single date after the year 1582 A.D. in the form of month, day, and year.
OPERATIONS
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Construct a new DateType instance.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Set the date.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Inspect the date's month.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Inspect the date's day.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Inspect the date's year.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Print the date.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Compare two dates for before, equal, or after.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Increment the date by one day.
Discussion: We create the DateType ADT in two stages: specification, followed by implementation. The result of the first stage is a C++ specification (.h) file containing the declaration of a DateType class. This file must describe for the user the precise semantics of each of the ADT operations.

 
< previous page page_869 next page >