(Enhancing Class Date) Modify the Date class of Figs. 9.22–9.23 to perform error checking on the initializer values for data members month, day and year. Also, provide a member function nextDay to increment the day by one. The Date object should always remain in a consistent state. Write a program that tests function nextDay in a loop that prints the date during each iteration to illustrate

that nextDay works correctly. Be sure to test the following cases:

a) Incrementing into the next month.
b) Incrementing into the next year.

```
#ifndef DATE_H
#define DATE_H

class Date
{
public:
Date( int = 1, int = 1, int = 2000 ); // default constructor
void print(); // print function
void setDate( int, int, int ); // set month, day, year
void setMonth( int ); // set month
void setDay( int ); // set day
void setYear( int ); // set year
int getMonth(); // get month
int getDay(); // get day
int getYear(); // get year
void nextDay(); // next day
private:
int month; // 1-12
int day; // 1-31 (except February(leap year), April, June, Sept, Nov)
int year; // 2000+
bool leapYear(); // leap year
int monthDays(); // days in month
}; // end class Date

#endif
```
```
// Member-function definitions for class Date.
#include
#include "Date.h" // include definition of class Date
using namespace std;

Date::Date( int m, int d, int y )
{
setDate( m, d, y ); // sets date
} // end Date constructor

void Date::setDate( int mo, int dy, int yr )
{
setMonth( mo ); // invokes function setMonth
setDay( dy ); // invokes function setDay
setYear( yr ); // invokes function setYear
} // end function setDate
void Date::setDay( int d )
{
if ( month == 2 && leapYear() )
day = ( d <= 29 && d >= 1 ) ? d : 1;
else
day = ( d <= monthDays() && d >= 1 ) ? d : 1;
} // end function setDay

void Date::setMonth( int m )
{
month = m <= 12 && m >= 1 ? m : 1; // sets month
} // end function setMonth

void Date::setYear( int y )
{
year = y >= 2000 ? y : 2000; // sets year
} // end function setYear

int Date::getDay()
{
return day;
} // end function getDay

int Date::getMonth()
{
return month;
} // end function getMonth

int Date::getYear()
{
return year;
} // end function getYear

void Date::print()
{
cout << month << '-' << day << '-' << year << '\n'; // outputs date
} // end function print

void Date::nextDay()
{
setDay( day + 1 ); // increments day by 1

if ( day == 1 )
{
setMonth( month + 1 ); // increments month by 1

if ( month == 1 )
setYear( year + 1 ); // increments year by 1
} // end if statement
} // end function nextDay

bool Date::leapYear()
{
if ( year % 400 == 0 || ( year % 4 == 0 && year % 100 != 0 ) )
return true; // is a leap year
else
return false; // is not a leap year
} // end function leapYear

int Date::monthDays()
{
const int days[ 12 ] =
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

return month == 2 && leapYear() ? 29 : days[ month - 1 ];
} // end function monthDays
```
```
#include
#include "Date.h" // include definitions of class Date
using namespace std;

int main()
{
const int MAXDAYS = 16;
Date d( 12, 24, 2004 ); // instantiate object d of class Date

// output Date object d's value
for ( int loop = 1; loop <= MAXDAYS; ++loop )
{
d.print(); // invokes function print
d.nextDay(); // invokes function next day
} // end for

cout << endl;
} // end main
```
12-24-2004
12-25-2004
12-26-2004
12-27-2004
12-28-2004
12-29-2004
12-30-2004
12-31-2004
1-1-2005
1-2-2005
1-3-2005
1-4-2005
1-5-2005
1-6-2005
1-7-2005
1-8-2005

Computer Science & Information Technology

You might also like to view...

You would use the ________ to temporarily hold selections that you have cut or copied

Fill in the blank(s) with correct word

Computer Science & Information Technology

You write a(n) ________ letter in response to a job opening that exists

Fill in the blank(s) with correct word

Computer Science & Information Technology