Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create files date.cpp and date.h , with a portable class called Date , for creating and using objects that will store valid dates of the

Create files date.cpp and date.h, with a portable class called Date, for creating and using objects that will store valid dates of the year. The instructions and details are as below:

image text in transcribedimage text in transcribed

image text in transcribed

image text in transcribed

1. An object of type Date should represent a calendar date in terms of month, day, and year, as on a 12-month A.D. calendar. The valid months are January through December, a valid day must correspond to a valid day for the given month, and the year must be a positive number no less than 1900. The class features (public interface) should work exactly as specified, regardless of what program might be using Date objects; Note: For purposes of easy input (from keyboard or into functions), date values will be specified with integers. Month values will be 1 for January, 2 for February, and so on up to 12 for December. A valid day value will be an integer between 1 and the number of days in the month (definitely no more than 31). Valid year values are positive integer numbers greater than or equal to 1900. 2. Your Date class must provide the following services (i.e. member functions). These functions will make up the interface of the class. Make sure you use function prototypes as specified here. (You may write other private functions you feel necessary, but the public interface must include all the functionality described below. For each function and each parameter, you need to think carefully if const is needed for declaration and definition. 2.1. Constructor(s): 1. Default constructor: allow a Date object to be declared without specified values, in which case it should be initialized to 1/1/2020. 2. Parameter constructor: allow a Date object to be declared with the user-specified values for the month, day, and year, using integer values. If any of the values would result in an invalid date, the constructor should throw out the erroneous information and initialize the object to represent 1/1/2020 (January 1, 2020) 3. Conversion constructor: allow a Date object to be constructed from a string (with the type of const char*) in a format of "month/day/year", where month, day, and year are string literals representing month, day, and year of a Date object, respectively, and the slash characters (/ with the ASCII code 47) is the delimiter. If any of the values would result in an invalid date, the constructor should throw out the erroneous information and initialize the object to represent 1/1/2020 (January 1, 2020). Examples: Date dl; Date d2 (3,4,1992); Date d3 (13,30, 1990); Date d4 ("2/02/2020"); Date d5 ("02/29/2019"); // initializes to Jan 1, 2020 // initializes to March 4, 1992 // invalid month, initializes to Jan 1, 2020 instead. // initializes to Feb. 2, 2020 // invalid date, initializes to Jan 1, 2020 instead. 2.2. void Input(): This function should prompt the user to enter a date (like "Input date in month/day/year format: "), and then allow the user to input a date from the keyboard. User input is expected to be in the format month/day/year, where month, day, and year are integer values. Whenever the user attempts to enter an invalid date, the Input function should display an appropriate error message (like "Invalid date. Try again: ") and make the user re-enter the whole date. A few examples of some good and bad inputs: Legal: Illegal: 1/4/2000, 2/28/1996, 12/31/1945 13/12/1985, 11/31/2002, 8/30/-2000 You may assume that the user entry will always be of the form: M/D/Y, where M, D, and Y are integers, and the slash characters / with the ASCII code 47) are always present. 2.3. int Get Month() int GetDay() int GetYear() These are "accessor" functions, and they should return the month, day, and year, respectively, to the caller. 2.4. bool Set(int m, int d, int y) This function should set the date to the specified values (the first parameter represents the month, the second represents the day, and the third represents the year). If the resulting date is an invalid date, the operation should abort (i.e. the existing stored date should not be changed). This function should return true for success and false for failure (i.e. invalid date sent in). 2.5. void Increment() This function should move the date forward by ONE calendar day. Examples: Date dl (10, 31, 1998); // Oct 31, 1998 Date d2 (2, 28, 2016); // Feb 28, 2016 dl.Increment (); d2.Increment (); // dl is now Nov 1, 1998 // d2 is now Feb 29, 2016 (a leap year) 2.6. void Decrement() This function should move the date backward by ONE calendar day. Examples: Date di (1, 1, 2020); Date d2 (3, 1, 2016); // Jan 1, 2020 // Mar 1, 2016 dl. Decrement (); d2. Decrement (); // dl is now Dec 31, 2019 // d2 is now Feb 29, 2016 (a leap year) Note that for the "first" date, 1/1/1900, as a special case, once we call the Decrement() function, the date actually doesn't change. 2.7. int DayofWeek() This function determines day of the week for a valid Date object, and the returned values range from 0 to 6: 0 stands for Sunday, 1 for Monday, ....., and 6 for Saturday. Here is one algorithm to compute day of the week, called Sakamoto's Algorithm, from Wikipedia for your reference: int dayofweek(y, m, d) /* 1 1752 (in the U.K.) */ static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4); y == n

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

More Books

Students also viewed these Databases questions