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

Which of the following password policy settings can be used to force users to change their password after a predefined period of time has passed?

A) Enforce password history B) Password complexity requirements C) Minimum password age D) Maximum password age

Computer Science & Information Technology

The precedence of an operator establishes its priority relative to all other operators.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

When you click the _____ Background button in the Adjust group on the Picture Tools Format tab, part of the photograph is marked to be removed and part of it is marked to be retained.

A. Edit B. Keep C. Merge D. Remove

Computer Science & Information Technology

Users always follow the expected path.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology