1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
#include "Date.h"
#include<iostream>
#include<string>
using namespace std;
Date::Date( int mn, int dy, int yr )
{
// validate the month
if ( mn > 0 && mn <= 12 )
{
month = mn;
}
else
{
// invalid month set to 1..
month = 1;
cout << "Invalid month (" << mn << ") set to 1.\n";
}
year = yr;
// validate the day by calling the utility function and assigne it to Data member day..
day = checkDay( dy );
cout << endl;
}
// print Date object in form month/day/year...
void Date::print() const
{
cout << "**You have registed in : "<<month << '/' << day << '/' << year<<"."<<endl<<endl;
}
// utility function to confirm proper day value based on month and year, & handles leap years,too...
int Date::checkDay( int testDay ) const
{
static const int daysPerMonth[ 13 ] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// determine whether testDay is valid for specified month..
if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
{
return testDay;
}
// February 29 check for leap year ..
if ( month == 2 && testDay == 29 && ( year % 400 == 0 || ( year % 4 == 0 && year % 100 != 0 ) ) )
{
return testDay;
// invalid Day set to 1..
cout << "Invalid day (" << testDay << ") set to 1.\n";
return 1;
}
}
|