Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

R9.2 (5 pts): Consider a Date class that stores a calendar date such as November

ID: 3551385 • Letter: R

Question

R9.2 (5 pts): Consider a Date class that stores a calendar date such as November 20, 2011. Consider two possible implementations of this class: 1. by storing the day, month, and year; and 2. by storing the number of days since January 1, 1900. Why might an implementator prefer the second version? How does the choice affect the user of the class? R9.3 (6 pts): Write a partial C++ class definition (not implementation) that contains the public interface of the Date class described in Exercise R9.2. Declare member functions for setting the date to a particular year, month, and day; for advancing the date by a given number of days; and for finding the number of days between this date and another. Pay attention to const. You do not need to include the private section or the method implementations. R9.2 (5 pts): Consider a Date class that stores a calendar date such as November 20, 2011. Consider two possible implementations of this class: 1. by storing the day, month, and year; and 2. by storing the number of days since January 1, 1900. Why might an implementator prefer the second version? How does the choice affect the user of the class? R9.3 (6 pts): Write a partial C++ class definition (not implementation) that contains the public interface of the Date class described in Exercise R9.2. Declare member functions for setting the date to a particular year, month, and day; for advancing the date by a given number of days; and for finding the number of days between this date and another. Pay attention to const. You do not need to include the private section or the method implementations.

Explanation / Answer

1)

An implementer might prefer the second version, where the date is stored as a number of days since a given date, because it is very simple to implement. That is, you could store the information in a single integer variable (for example). The choice of implementation should not affect the users of the class, because the change can be made without changing the public interface. The public interface can still provide a date as day, month, and year, even if the underlying implementation is storing the number of days since January 1, 1900.

2)


class Date

{

public:


/**     Sets the date to that specified by the arguments.

      parameter day the day of the month (1-31)

      parameter month the month number (1-12)

      parameter year the four-digit year

   */

   void set_date(int day, int month, int year);

   /**

      Advances the current date by the number of days specified.

      @parameter days the number of days to advance

   */

   void advance(int days);

   /**

    Calculates number of days between current date and

      date specified by arguments.

      @parameter day the day of the month (1-31)

      @parameter month the month number (1-12)

      @parameter year the four-digit year

   */

   int days_between_this_and(int day, int month, int year) const;