<pre name="code" class="cpp">#include<iostream>
using namespace std;
class Date {
public:
Date(){}
Date(int m,int d,int y)
{
month=m;
day=d;
year=y;
}
void print()
{
cout<<year<<"."<<month<<"."<<day<<endl;
}
Date operator +(int);
private:
int month,day,year;
};
static int days[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};
int a(int year)
{
if((year%4==0&&year%100!=0)||(year%400==0))
return 1;
else return 0;
}
Date Date::operator+(int m)
{
int b;
b=0;
b=a(this->year);
m+=this->day;
while (m>days[b][this->month-1])
{
m-=days[b][this->month-1];
if (++(this->month)==13)
{
this->month=1; //当前的对象(this所指的)的成员month,被赋值1
(this->year)++; //当前的对象(this所指的)的成员year,加1
b=a(this->year);
}
}
this->day=m; //当前的对象(this所指的)的成员day,被赋值为m
return *this; //返回类对象本身
}
int main()
{
Date d1(2,28,2012),d2;
d1.print();
d2=d1+400;
d2.print();
return 0;
}
7.11
最新推荐文章于 2024-07-15 17:58:02 发布