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.
StatementEffect
cout << d1 << endl;"October 17, 1953" is printed.
if (d1 < d2) cout << "ok" << endl;"ok" is printed
d2++;d2 now represents "November 1, 1995"

  1. 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

  2. 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 d1Date Represented by d2Result Returned by DaysApart(d1,d2)
    February 18, 1991February 18, 19910
    Feburary 18, 1991February 20, 19912
    February 20, 1991February 18, 19912
    January 31, 1991February 5, 19915
    December 30, 1991January 2, 19923

    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

  3. Consider two alternative ways to implement the private section of the class Date:
    1. Using three integers: one representing the year, one the month, and one the day
    2. 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.)

    1. 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 ++
      
      
      
    2. 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