|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<iostream>
using namespace std;
class CDate
{
private:
int year,month,day;
public:
void Show();
CDate()
{
year=2015;
month=5;
day=7;
cout<<"Default constructor is called!"<<endl;
}
void fun(CDate &p,int year,int month,int day)
{
year=p.year;
month=p.month;
day=p.day;
}
CDate(int y,int m,int d);
void SetDate(int y,int m,int d);
int IsLeapYear(int y);
int DaysOfMonth(int y,int m);
CDate IncOneDay();
int GetYear()
{
return year;
}
int GetMonth()
{
return month;
}
int GetDay()
{
return day;
}
};
CDate::CDate(int y,int m,int d)
{
cout<<"Constructor is called!"<<endl;
year=y;
month=m;
day=d;
}
void CDate::SetDate(int y,int m,int d)
{
year=y;
month=m;
day=d;
cout<<"Date is reset!"<<endl;
}
int CDate::IsLeapYear(int y)
{
if((y%400==0)||((y%4==0)&&(y%100!=0)))
return 1;
else
return 0;
}
int CDate::DaysOfMonth(int y,int m)
{
int i,t;
t=IsLeapYear(y);
switch(m)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
i=31;
break;
case 2:
i=27+t;
break;
case 4:
case 6:
case 9:
case 11:
i=30;
break;
}
return i;
}
CDate CDate::IncOneDay()
{
int y=year,m=month,d=day;
int i,t=0;
i=DaysOfMonth(y,m);
if(d+1>i)
{
d=1;
m+=1;
}
if(m>12)
{
y+=1;
m=1;
}
return CDate(y,m,d);
}
void CDate::Show()
{
cout<<year<<"-"<<month<<"-"<<day<<endl;
}
int main()
{
CDate d1;
d1.Show();
d1.SetDate(2015,2,28);
d1.IncOneDay().Show();
d1.Show();
CDate d2(2016,12,31);
d2.Show();
d1=d2.IncOneDay();
d1.Show();
d2.Show();
d1.SetDate(d2.GetYear(),d2.GetMonth(),d2.GetDay());
d1.Show();
return 0;
}
如何在调用inconeday函数的时候不要调用构造函数,也能达到相同效果,并且输出差不多,只是少了几次Constructor is called! |
|