(1)写一个表示日期的类;
①构造函数重载:
不指定年时默认为2001年;
不指定月时默认为1月;
不指定日时默认为1日;
写出无参数的构造函数、指定年的构造函数、
指定年月的构造函数、指定年月日的构造函数、
拷贝构造函数;
#include <iostream>
using namespace std;
class DateTimer
{
public:
DateTimer(int year)
{
m_year=year;
}
DateTimer(int year,int month)
{
m_year=year;
m_month=month;
}
DateTimer(int year,int month,int day)
{
m_year=year;
m_month=month;
m_day=day;
}
DateTimer(){}
void operator=(DateTimer dateTime)
{
this->m_year=dateTime.m_year;
this->m_month=dateTime.m_month;
this->m_day=dateTime.m_day;
}
void Show()
{
cout<<"Year = "<<m_year<<" Month = "<<m_month<<" Day = "<<m_day<<endl;
}
~DateTimer(){}
private:
int m_year=2001,m_month=1,m_day=1;
};
int main()
{
DateTimer time1(2002,10,21);
DateTimer time2=time1;
time1.Show();
time2.Show();
}