Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Design and implement a Date class based on Figure 2.8, subject to the following restrictions and suggestions. Each Date must be stored as a single

image text in transcribed

Design and implement a Date class based on Figure 2.8, subject to the following restrictions and suggestions.

Each Date must be stored as a single integer equal to the number of days since the fixed base date, January 1, 1000 (or some other date if you prefer). Call that data member m_DaysSinceBaseDate.

The base year should be stored as a static int data member (e.g., 1000 or 1900).

The class has a constructor and a set function that have month, day, year parameters. These three values must be used to compute the number of days from the base date to the given date. We have specified a private member function named ymd2dsbd() to do that calculation for both.

The toString() function returns a representation of the stored date in some standard string format that is suitable for display (e.g., yyyy/mm/dd). This involves reversing the computation used in the ymd2dsbd()function described above. We have specified a private member function named getYMD() to do that calculation. We have also suggested a parameter for the toString() function (bool brief) to provide a choice of date formats.

We have specified some static utility functions (e.g., leapyear()) that are static because they do not affect the state of any Date objects.

Make sure you use the correct rule for determining whether a given year is a leap year!

Create a file named date.h to store your class definition.

Create a file named date.cpp that contains the definitions of all the functions declared in date.h.

Write client code to test your Date class thoroughly.

Your class should handle "invalid" dates in a reasonable way (e.g., year earlier than the base year, month or day out of range, etc.).

Here is the code for setToToday() that makes use of the system clock to determine today's date. You need to #include (from the C Standard Library) to use this code.

void Date::setToToday() { struct tm *tp = 0; time_t now; now = time(0); tp = localtime(&now); set(1900 + tp->tm_year, 1 + tp->tm_mon, tp->tm_mday); } 

getWeekDay() function returns the name of the week day corresponding to the stored date. Use this in the fancy version of toString(). Hint: Jan 1, 1900, was a Monday.

Date m DaysSinceBaseDate: int + Date() Date(y int, m int, d: int) +set(y: int, m: int, d: int) +toString(brief: bool): string setToToday() +getWeekDay) string + lessThan(d: const Date&): bool + equals(d: const Date&): bool + daysBetween(d: const Date&) int + addDays(days: int) Date + leapYear(vear: int) : bool + monthName(month : int): string + yearDays(year: int): int + monthDays(month : int, year: int): int ymd2dsbd(y: int, m: int, _d: int): int getYMD(y : int&, m: int&, d: int&)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions