This code is getting the following error:
"Date: multiple default constructors defined"
I have Date();
and Date(int=1,int=1,int=1990); in the header, but not sure why it's wrong?
CODE
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
class Date {
public:
Date();
Date( int = 1, int = 1, int = 1900 ); // default constructor
void FormatTime();
void print() const; // print date in month/day/year format
~Date(); // provided to confirm destruction order
private:
int month; // 1-12 (January-December)
int day; // 1-31 based on month
int year; // any year
int myTime;
// utility function to test proper day for month and year
int checkDay( int ) const;
}; // end class Date
Date::Date()
{
myTime=time(0); //to take the current time
FormatTime();
cout<<"The system Time is:"<<endl;
cout<<endl;
}
// constructor confirms proper value for month; calls
// 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 { // invalid month set to 1
month = 1;
cout << "Month " << mn << " invalid. Set to month 1.\n";
}
year = yr; // should validate yr
day = checkDay( dy ); // validate the day
// output Date object to show when its constructor is called
print();
cout << endl;
}
void Date::FormatTime()
{
int century;
century = myTime/(60*60*24*30*12*100);
year=(myTime-(century*60*60*24*30*12*100))/(60*60*24*30*12);
month=(myTime-(century*60*60*24*30*12*100)-(year*60*60*24*30*12))/(60*60*24*30);
day=(myTime-(century*60*60*24*30*12*100)-(year*60*60*24*30*12)
-(month*60*60*24*30))/(60*60*24);
}
// print Date object in form month/day/year
void Date::print() const
{
cout <<setw(3)<<setfill('0')<<day<<" "<<year<<endl;//DDD YYYY
cout <<setw(2)<<setfill('0')<<month<< '/'
<<setw(2)<<setfill('0')<<day << '/' << year%100<<endl; //MM/DD/YY
switch(month)
{
case 1:
cout<<"January ";
break;
case 2:
cout<<"February ";
break;
case 3:
cout<<"March ";
break;
case 4:
cout<<"April ";
break;
case 5:
cout<<"May ";
break;
case 6:
cout<<"June ";
break;
case 7:
cout<<"July ";
break;
case 8:
cout<<"August ";
break;
case 9:
cout<<"September ";
break;
case 10:
cout<<"October ";
break;
case 11:
cout<<"November ";
break;
case 12:
cout<<"December ";
break;
}//end switch
cout << day <<", "<< year << endl;
} // end function print
// output Date object to show when its destructor is called
Date::~Date()
{
} //empty destructor
// 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;
cout << "Day " << testDay << " invalid. Set to day 1.\n";
return 1; // leave object in consistent state if bad value
} // end function checkDay
int main()
{
Date D1;
D1.FormatTime();
D1.print();
Date D2(6,14,1992); //month,day,year
cout<<endl;
Date D3(10,3);
cout<<endl;
return 0;
}
Thanks for the help!