#include<iostream>
using namespace std;
class data
{
private:
int m, d, y;
static int day[];
public:
data(int M, int D, int Y)
{
m = M;
d = D;
y = Y;
}
void display()
{
cout << "年" << y << "月" << m << "日" << d << endl;
}
data operator+(int n)const
{
data Data = *this;
n += Data.d;
while (n > day[Data.m - 1])
{
n -= day[Data.m - 1];
if (++Data.m == 13)
{
Data.m = 1;
Data.y++;
}
}
Data.d = n;
return Data;
}
};
int data::day[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int main()
{
data d1(03, 05, 2012);
d1.display();
data d2 = d1 + 10;
cout << "加上后" << endl;
d2.display();
system("pause");
return 0;
}
类的加法
最新推荐文章于 2021-04-23 22:22:15 发布