问答题
请使用”答题”菜单或使用VC6打开考生文件夹proj2下的工程proj2,本程序中有两个类:一是日期类(Date),用于表示一天的日期(年、月、日);另一个是截止日期类(Dead.1ine),用于表示一个任务的截止日期,超过该日期即为超时。Deadline类中有Date类的数据成员。这里对Deadline类的数据成员和成员函数做一下说明:int id;//截止日期的idDate end—date;//截止日期,表示允许完成该任务的最后一天 void finish(Date date);//date未超过截止日期时,输出”Finished!”,否则输出”Time out.” bool check(Date date);//date未超过截止日期时返回true,否则返回false。 程序正确情况下输出: Finished! Time out.注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不能删除或移动“//**********found**********”。#include<iOStream>using namespace std;class Date(private:int year;int month;int day.of month;public:Date(int Y,int m,int d){this一>year=Y;.this一>month:m;this一>day Of month=d;}bool operator<=(Date&dt)const {if(this一>year<dt.year lI(this一>year==dt.year&&this一>month<dt.month)I I(this一>year==dt.year&&this一>month==dt.month&&this一>day—of—month<=dt.day_of_month))//**********found**********};return false;}};class Deadline{private:int id;Date end date;public:Deadline(int id,int year,int month,int day_of_month)//**********found**********{this一>id=id;}void finish(Date date){if(check(date))//**********found**********cout<<<<endl;elsecout<<”Time out.”<<endl;)bool check(Date date){// **********found**********return——;:}};int main(){Deadline * d1=new Deadline(1,2014,3,12);Deadline*d2=new Deadline(2,2013,12,20);Date current time(2 01 4,1,1);dl一>finish(current time);d2一>finish(current time);delete d1,delete d2;return 0;}
【正确答案】正确答案:(1)return true (2):end—date(year,month,day-0f_month) (3)”Finshed” (4)(end—date<=date)?false:true
【答案解析】解析:主要考查类构造函数定义,初始化列表;条件运算符的使用。 【解题思路】 (1)根据题设可知比较两个日期的大小,由if语言的判断,可知this的日期大于date,返回真。 (2)构造函数的初始化列表,使用基类的构造函数完成子类成员的初始化。 (3)根据题意可知,没有超过deadline,故输出“Finshed”。 (4)检查当前日期是否超过截止时间。超过deadline,返回false,否则返回true。