Code:
// DateClass.cpp: implementation of the DateClass class.
//
//////////////////////////////////////////////////////////////////////
#include <cstring>
#include "DateClass.h"
#include <iostream>
using namespace std;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
// Default constructor for DateClass objects
DateClass::DateClass(int mm, int dd, int yy)
{
cout << "Default constructor called\n";
setDate(mm, dd, yy);
}
DateClass::~DateClass()
{
cout << "\nDestructor called\n";
}
void DateClass::setDate(int mn, int dy, int yr) // set date
{
setMonth(mn);
setDay(dy);
setYear(yr);
}
void DateClass::setDay(int d) // check for valid month/day & leap year and set day
{
static const int lastDay[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (month == 2 && d == 29 && (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)))
day = d;
else if (d > 0 && d <= lastDay[month])
day = d;
else
day = 1;
}
void DateClass::setMonth(int m) // check month for validity and set
{
month = ((m > 0 && m < 13) ? m : 1);
}
void DateClass::setYear(int y) // check year for validity and set
{
year = ((y >= 0 && y <= 9999) ? y : 2000);
}
void DateClass::printNumeral() // print in mm/dd/yy format
{
cout << "Date in mm/dd/yy format: \n";
cout << month << "/" << day << "/" << year << endl;
}
void DateClass::printStandard() // print in month day, year format
{
// array of month names
static char* mnthName[12] = {"January", "February", "March", "April","May",
"June", "July", "August", "September", "October", "November", "December"};
cout << "Date in standard format: \n";
cout << mnthName[month - 1] << " " << day << ", " << year << endl;
}
DateClass::DateClass(char *mo, int da, int ye)
{
cout << "Non-default constructor called\n";
int i = 0, m = 0; // counter and month variables
// array of month names
static char* mnthName[12] = {"January", "February", "March", "April","May",
"June", "July", "August", "September", "October", "November", "December"};
// convert character month name to integer for setDate function
do
{
if (strcmp(mo, mnthName[i]) == 0)
m = i + 1;
i++;
}while (m == 0);
setDate(m, da, ye);
}