Free Response
1996 #1
Consider a Date class used for storing and manipulating dates. The following operators are overloaded for the class Date: <<, ==, >, <, ++ . Only operator ++ is a member function, the other operators are not member functions.
ostream & operator<< (ostream & os, Date d);
//prints date represented by d
bool operator == (Date lhs, Date rhs);
// returns true if and only if lhs and rhs represent same date
bool operator < (Date lhs, Date rhs);
// returns true if and only if lhs occurs before rhs
bool operator > (Date lhs, Date rhs);
//returns true if and only if lhs occurs after rhs
Date operator ++(int);
//increments date by one day
The following statements illustrate the use of these operators, where d1 represents the date October 17, 1953 and d2 represents the date October 31, 1995.
Statement | Effect
|
cout << d1 << endl;"October 17, 1953" is printed.
| if (d1 < d2) cout << "ok" << endl;"ok" is printed
| d2++;d2 now represents "November 1, 1995" | | | |
- Write the function PrintOneWeekLater, whose header is given below. PrintOneWeekLater prints a date that is seven days later than its parameter. For example, if the variable d represents the date February 18, 1991, then PrintOneWeekLater(d) prints February 25, 1991. In writing PrintOneWeekLater, you may use the overloaded (Date) operators <<, ==, <, >, ++. All manipulations of Date variables must be performed using only these operators.
Complete OneWeekLater below the following header
void PrintOneWeekLater(Date d)
// postcondition: Prints a date that is seven days later than the date represented by d
- Write the function DaysApart, whose header is given below. DaysApart returns the number of days separating the dates represented by its two parameters, regardless of which date is earlier. (If the two parameters represent the same date, DaysApart returns 0.)
For example:
Date Represented by d1 | Date Represented by d2 | Result Returned by DaysApart(d1,d2)
February 18, 1991 | February 18, 1991 | 0
| Feburary 18, 1991 | February 20, 1991 | 2
| February 20, 1991 | February 18, 1991 | 2
| January 31, 1991 | February 5, 1991 | 5
| December 30, 1991 | January 2, 1992 | 3 | |
In writing DaysApart you may use the overloaded (Date) operators <<, ==, <, >, ++. All manipulations of Date variables must be performed using only these operators.
Complete function DaysApart below the following header
int DaysApart(Date d1, Date d2)
// postcondition: returns 0 if d1 and d2 represent the same date; otherwise, returns the number of days separating the dates represented by d1 and d2
- Consider two alternative ways to implement the private section of the class Date:
- Using three integers: one representing the year, one the month, and one the day
- Using a single integer, representing the total number of days from January 1 of the year 1 to the given date (Assume that an int is large enough to store such a value.)
- Which of the the operators <<, <, ++ should require fewer operations when alternative I is used than when alternative II is used? Circle each of the operators that requires fewer operations for its implementation when alternative I is used, and give a brief justification for each one you circle
justification
operator <<
operator <
operator ++
- Which of the the operators <<, <, ++ should require fewer operations when alternative II is used than when alternative I is used?
Circle each of the operators that requires fewer operations for its implementation when alternative II is used, and give a brief justification for each one you circle
justification
operator <<
operator <
operator ++
Continue to: Unit 3
/ Prev
/ Next