I need to make a member function nextday to increment the day by one. The Date object should always remain in a consistant state. Need code that tests the nextday function in a loop that prints the date during each iteration of the loop to illustrate that the nextday function works correctly. Have to test incrementing into the next month and next year.
Here is what I have so far.
Thanks a lot!!!!!!!
#ifndef DATE1_H
#define DATE1_H
class Date {
public:
Date( int = 1, int = 1, int = 1900 ); // default constructor
void print() const; // print date in month/day/year format
~Date(); // provided to confirm destruction order
nextday(int,int,int);// increment values
private:
int month; // 1-12
int day; // 1-31 based on month
int year; // any year
// utility function to test proper day for month and year
int checkDay( int );
};
#endif
// date1.cpp
// Member function definitions for Date class.
#include <iostream>
using std::cout;
using std::endl;
#include "date1.h"
// Constructor: Confirm proper value for month;
// call utility function checkDay to confirm proper
// value for day.
Date::Date( int mn, int dy, int yr )
{
if ( mn > 0 && mn <= 12 ) // validate the month
month = mn;
else {
month = 1;
cout << "Month " << mn << " invalid. Set to month 1.\n";
}
year = yr; // should validate yr
day = checkDay( dy ); // validate the day
cout << "Date object constructor for date ";
print(); // interesting: a print with no arguments
cout << endl;
}
// Print Date object in form month/day/year
void Date::print() const
{ cout << month << '/' << day << '/' << year; }
Destructor: provided to confirm destruction order
Date::~Date()
{
cout << "Date object destructor for date ";
print();
cout << endl;
}
///////// This is my problem area need a loop to test here////////
Date::nextday(int mn, int dy, int yr)
{ day += dy;
month += mn;
year += yr;
print();
}
// Utility function to confirm proper day value
// based on month and year.
// Is the year 2000 a leap year?
int Date::checkDay( int testDay )
{
static const int daysPerMonth[ 13 ] =
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
return testDay;
if ( month == 2 && // February: Check for leap year
testDay == 29 &&
( year % 400 == 0 ||
( year % 4 == 0 && year % 100 != 0 ) ) )
return testDay;
cout << "Day " << testDay << " invalid. Set to day 1.\n";
return 1; // leave object in consistent state if bad value
}