求C++程序设计:给出一个日期类的定义。其中包含month,day和year三个属性,以及显示日期的函数。
2个回答
展开全部
个人认为应该加入日期检测功能
#include <iostream>
using namespace std;
class datestruct
{
public:
void ShowDate(void);
void SetDate(void);
private:
int year;
int month;
int date;
static int days1[];
static int days2[];
};
int datestruct::days1[]={31,28,31,30,31,30,31,31,30,31,30,31};//非闰年
int datestruct::days2[]={31,29,31,30,31,30,31,31,30,31,30,31};//闰年
void datestruct::ShowDate(void)
{
cout<<"The date is "<<year<<"."<<month<<"."<<date<<endl;
}
void datestruct::SetDate(void)
{
int tyear;
int tmonth;
int tdate;
bool flag;
cout<<"Please enter the year:";
cin>>tyear;cout<<endl;
if((tyear%4==0&&tyear%100!=0)||tyear%400==0)
flag=true;
else
flag=false;
cout<<"Please enter the month:";
while(1)
{
cin>>tmonth;
if(tmonth>12||tmonth<1)
{
cout<<"Please enter an integer between 1~12!"<<endl;
cout<<"Please enter the month:";
continue;
}
else
break;
}//检查月份,设置处理非法月份的输入循环
cout<<"Please enter the date:";
while(1)
{
cin>>tdate;
if(flag)
{
if(tdate<1||tdate>days2[tmonth-1])
{
cout<<"An illegal date!"<<endl;
cout<<"Please enter the date:";
continue;
}
else
break;
}
else
{
if(tdate<1||tdate>days1[tmonth-1])
{
cout<<"An illegal date!"<<endl;
cout<<"Please enter the date:";
continue;
}
else
break;
}
}//检查日期,包括对于闰年日期的检查
this->date=tdate;
this->month=tmonth;
this->year=tyear;
}
int main(void)
{
datestruct myDate;
myDate.SetDate();
myDate.ShowDate();
return 0;
}
2013-09-18
展开全部
#include <iostream>
using namespace std;
class Date
{
private:
int year,month,day;
public:
Date(int year,int month,int day)
{
this->year = year;
this->month = month;
this->day = day;
}
void show()
{
cout<<year<<"年"<<month<<"月"<<day<<"日";
}
};
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询