你确定是警告吗?
构造函数还能这样用?Date{2001,2,5}
构造函数也是函数啊,为什么不是小括号,却是大括号?
难道不应该是Date(2001,2,5)吗?#include<iostream>
using namespace std;
int howday(int i)
{
switch(i)
{
case 1:i=31;break;
case 2:i=59;break;
case 3:i=90;break;
case 4:i=120;break;
case 5:i=151;break;
case 6:i=181;break;
case 7:i=212;break;
case 8:i=243;break;
case 9:i=273;break;
case 10:i=304;break;
case 11:i=334;break;
case 12:i=365;break;
}
return i;
}
class Date
{
public:
Date(int y,int m,int d):Year(y),Month(m),Day(d){}
void SetDate();
void GetDate();
int DiffDate();
void AddDate(int num);
private:
int Year;
int Month;
int Day;
};
void Date::SetDate()
{
cout<<"请输入年"<<endl;
cin>>Year;
cout<<"请输入月"<<endl;
cin>>Month;
cout<<"请输入日"<<endl;
cin>>Day;
}
void Date::GetDate()
{
cout<<Year<<"."<<Month<<"."<<Day<<endl;
}
int Date::DiffDate()
{
int y=2020;
int m=4;
int d=9;
y=(y-this->Year)*365;
m=howday(m)-howday(this->Month);
d=d-this->Day;
return y+m+d;
}
void Date::AddDate(int num)
{
this->Day+=num;
while(this->Day>31)
{
Day-=30;
Month++;
}
while(this->Month>12)
{
Month-=12;
Year++;
}
cout<<"改变后的日期是: "<<this->Year<<"."<<this->Month<<"."<<this->Day<<endl;
}
int main()
{
class Date D[3]={Date(2001,2,5),
Date(2001,6,1),
Date(2001,5,6)
};//构造函数也是函数啊,为什么不是小括号,却是大括号?
class Date *pDate;
int i=0;
pDate=D;
/*for(;i<3;i++)
{
D[i].SetDate();
}*/
D[1].GetDate();
(pDate+1)->GetDate();
i=D[2].DiffDate();
cout<<"过了"<<i<<"天"<<endl;
D[0].AddDate(10);
return 0;
}
|