应用题 1.  请使用菜单命令或直接用VC6打开考生文件夹下的工程proj3,其中声明了Date类,它是一个用于表示日期的类。成员函数isLessThan用以比较两个日期的大小:当第一个日期早于第二个日期时,返回true,否则返回false。请补充完整函数isLessThan。在main函数中给出了一组测试数据,此情况下程序的输出应该是:
    2007-06-21<2007-07-03
    2007-06-21>=2007-06-19
    2007-06-21<2010-01-01
    注意:只需在函数isLessThan的//********333********和//********666********之间填入若干语句,不要改动程序中的其他内容。
    #include"Date.h"
    int main(){
    Date date1(2007, 6, 21), date2(2007, 7, 3), date3(2007, 6, 19), date4(2010, 1, 1);
    date1.show();
    date1.isLessThan(date2)?cout<<"  <":cout<<">=";
    date2.show(); cout<<endl;
    date1.show();
    date1.isLessThan(date3)?cout<<"<":cout<<">=";
    date3.show(); cout<<endl;
    date1.show();
    date1.isLessThan(date4)?cout<<" <":cout<<">=";
    date4.show(); cout<<endl;
    writeToFile("c:\test\"); //不用考虑此语句的作用
    return 0;
    }
    //proj3\Date.cpp
    #include"Date.h"
    void Date::show(ostream&os){
    os<<getYear()<<'-'<<setfill('0')<<setw(2)<<getMonth()<<'-'<<setw(2)<<getDay();
    }
    bool Date::isLessThan(Date date)const{
    //********333********
    //********666********
    }
    //proj3\Date.h
    #include<iostream>
    #include<iomanip>
    using namespace std;
    class Date{
    int year;
    int month;
    int day;
    public:
    Date(int y, int m, int d):year(y), month(m), day(d){}
    int getYear()const{return year; }
    int getMonth()const{return month; }
    int getDay()eonst{return day; }
    void show(ostream&os=cout);
    bool operator==(Date date)const{
    return year==date.year&&month==date.month&&day==date.day;
    }
    bool isLessThan(Date date)const;
    };
    void writeToFile(const char*path);
【正确答案】bool less=false;
   if(year<date.getYear()|| year==date.getYear()&&month<date.getMonth()||year==
   date.getYear()&&month==date.getMonth()&&day<date.getDay())
   {
   less=true;
   }
   return less;
【答案解析】 本题考查的是Date类,其中涉及布尔变量、成员函数的使用、逻辑运算符和关系运算符的使用。
   主要考查考生对成员函数、关系运算符和逻辑运算符的掌握,成员函数isLessThan是将该对象本身与参数date进行比较,返回是否小于的布尔值。为了比较isLessThan的调用对象与date的大小,需要依次比较year、month、day三个整数,由于date的year、month、day都是私有成员,所以不能在islessThan中直接使用date的私有成员,而是应该使用date的共有成员函数来返回这些值,再将本身的成员与返回值比较,同时,为了比较时间大小,应该首先将year进行比较,接着是month,最后是day,并将比较结果暂存布尔变量less中,最后返回。