关系运算符重载实例2015
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include
//using namespace std;
class Date
{
private:
int year;
int month;
int day;
public:
Date(){}
Date(int y,int m,int d);
void display();
friend bool operator >(Date &d1,Date &d2);
friend bool operator <(Date &d1,Date &d2);
friend bool operator ==(Date &d1,Date &d2);
};
Date::Date(int y,int m,int d)
{
this->year=y;
this->month=m;
this->day=d;
}
void Date::display()
{
cout<
bool operator >(Date &d1,Date &d2)
{
if(d1.year>d2.year)
return true;
else if(d1.year==d2.year)
{
if(d1.month>d2.month)
return true;
else if(d1.month==d2.month)
{
if(d1.day>d2.day)
return true;
else return false;
}
else
return false;
}
else
return false;
}
bool operator <(Date &d1,Date &d2)
{
if(d1.year return true; else if(d1.year==d2.year) { if(d1.month return true; else if(d1.month==d2.month) { if(d1.day return true; else return false; } else return false; } else return false; } bool operator ==(Date &d1,Date &d2) { bool b; b=d1.year==d2.year&&d1.month==d2.month&&d1.day==d2.day; return b; } void Compare(Date &d1,Date &d2) { d1.display(); if(d1>d2) cout<<"大于"; if(d1 cout<<"小于"; if(d1==d2) cout<<"等于"; d2.display(); cout<<"\n"; } int main() { Date d1(2015,11,15),d2(2015,9,5); Date d3(2015,5,1),d4(2015,5,7); Date d5(2015,11,15),d6(2015,11,15); Compare(d1,d2); Compare(d3,d4); Compare(d5,d6); return 0; } 程序执行结果为: 2015-11-15大于2015-9-5 2015-5-1小于2015-5-7 2015-11-15等于2015-11-15