初学C++,这是一个构造函数重载的小小程序。int 造成了溢出,我到现在还不知道是怎么一回事?把红色标志的int去掉输入就正确了。WHY?
#include<iostream.h>
class date{
int dayth;
public:
date(int day);
date(int day,int month);
date(int day,int month,int year);
int getdate();
};
date::date(int day){
dayth=day;
}
date::date(int day,int month){
int i;
int th[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int dayth=0;
for(i=0;i<month;i++)
{dayth=dayth+th[i];}
dayth=dayth+day;
}
date::date(int day,int month,int year){
int i;
int th[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int dayth=0;
if((year%4==0&&year%100!=0)||year%400==0)
{
for(i=0;i<month;i++)
{dayth=dayth+th[i];}
dayth=dayth+day;
if(month>2)
dayth=dayth+1;
}
else{
for(i=0;i<month;i++)
{dayth=dayth+th[i];}
dayth=dayth+day;
}
}
int date::getdate(){
return dayth;
}
int main(){
int day;
int month;
int year;
cout<<"输入日期:";
cin>>day;
cout<<"输入月份:";
cin>>month;
cout<<"输入年份:";
cin>>year;
date q1(day);
date q2(day,month);
date q3(day,month,year);
cout<<"此日是一年的第"<<q1.getdate()<<"天\n";
cout<<"此日是一年的第"<<q2.getdate()<<"天\n";
cout<<"此日是一年的第"<<q3.getdate()<<"天\n";
return 1;
}