Overloading constructors?
I've written a Date class, implementation below. It has a default constructor and can print the date in either mm/dd/yyyy format, or as January 1, 2000, e.g. This is ok, it works. But I have to write an overloaded constructor so that besides setting the date with setDate(int, int, int) as default, it will accept the January.... format. The text we're using, Deitel's, says it can be done, but shows no examples of how. If I had a char, int, int constructor, would I then have to write new set and print functions for it? How does this work? We're also supposed to use ddd yyyy format, but I've never heard of that. Thanks.
Code:
// DateClass.cpp: implementation of the DateClass class.
//
//////////////////////////////////////////////////////////////////////
#include "DateClass.h"
#include <iostream>
using namespace std;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
DateClass::DateClass(int mm, int dd, int yy)
{
cout << "Constructor called\n";
setDate(mm, dd, yy);
}
DateClass::~DateClass()
{
cout << "\nDestructor called\n";
}
void DateClass::setDate(int mn, int dy, int yr)
{
setMonth(mn);
setDay(dy);
setYear(yr);
}
void DateClass::setDay(int d)
{
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)
{
month = ((m > 0 && m < 13) ? m : 1);
}
void DateClass::setYear(int y)
{
year = ((y >= 0 && y <= 9999) ? y : 2000);
}
void DateClass::printNumeral()
{
cout << "Date in mm/dd/yy format: \n";
cout << month << "/" << day << "/" << year << endl;
}
void DateClass::printStandard()
{
static const 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;
}