c++判断日期属于第几天程序的几个小问题
#include <iostream.h>
class CDate
{
private:
int Year,Month,Day;
static int LeapYearDaysInMonth[];
static int NormalYearDaysInMonth[]; //①
//int number=0; //②-1
public:
CDate(int year,int month,int day)
{
Year=year;
Month=month;
Day=day;
// number=0;
}
bool IsLeapYear(int Year)
{
if (Year%4==0 && Year%100!=0 || Year%400==0)
return true;
else return false;
}
int getdaynumber()
{
int number=0; //②-2
int *pDaysInMonth;
if(IsLeapYear(Year)==true)
{
pDaysInMonth=LeapYearDaysInMonth;
for(int i=0;i<=Month-1;i++)
{
number+=*(pDaysInMonth+i);
}
number+=Day;
return number;
}
else
{
pDaysInMonth=NormalYearDaysInMonth;
for(int i=0;i<=Month-1;i++)
{
number+=*(pDaysInMonth+i);
}
number+=Day;
return number;
}
}
};
int CDate::LeapYearDaysInMonth[]={0,31,29,31,30,31,30,31,31,30,31,30,31};
int CDate::NormalYearDaysInMonth[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; //①
void main()
{
int year,month,day;
cout<<"please input year"<<endl;
cin>>year;
cout<<"please input month"<<endl;
cin>>month;
cout<<"please input day"<<endl;
cin>>day;
CDate date(year,month,day);
cout<<"this day is the"<<date.getdaynumber()<<"th day"<<endl;
}
问题①
我知道不能在类内部直接初始化,因此,我在类内仅仅声明了数组,在类外对其进行了初始化,可是编译器提示要加static,请问这是为什么?
问题②
同样是变量初始化的问题,在②-1处我知道不能进行初始化,因此我在构造函数中对number进行赋初值操作,但是我发现如果在②-2赋初值却可以,请教这是为什么?
另外,关于number这个返回值变量我应该定义成全局变量还是局部变量呢?