如何运用重载运算符解决这个问题
要求增加成员函数实现:⑴ 能比较两个日期的大小(重载“= =”、“>”、“<”、“>=”、“<=”、“!=”);
⑵ 编写main( )函数测试,内容自定。
以下是我的原程序,该如何添加
#include <iostream.h>
#include <string.h>
class time
{
private:
int hour,min,sec;
public:
time(int h,int m,int s)
{hour=h;min=m,sec=s;}
void cool ()
{cout<<hour<<":"<<min<<":"<<sec;
if (hour>12)
cout<<"pm"<<endl;
else
cout<<"am"<<endl;
}
};
#include < iostream.h >
#include < string.h >
class date
{private:
int year,month,day;
public:
date(int y,int M,int d)
{year=y;month=M;day=d;}
void cool ()
{cout<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
if((year%4==0)&&(year%100!=0)||(year%400==0))
cout<<"是闰年"<<endl;
else
cout<<"不是闰年"<<endl;
}
};
class datetime:public date,public time
{public:
datetime(int y,int M,int d,int h,int m,int s):date( y, M, d),time( h, m, s)
{}
void cool ()
{
date:: cool ();
time:: cool ();
}
};
void main()
{
datetime A(1998,8,17,9,51,30);
A.cool();
}