#include<iostream>
using namespace std;
class Date
{
public:
int month, day, year;
public:
Date(int m = 0, int d = 0, int y = 0) :month(m), day(d), year(y){}
int operator==(Date& dt)const;
int operator!=(Date& dt)const;
//应该把所有的关系操作符一起重载
int operator<(Date& dt)const;
int operator>(Date& dt)const;
int operator<=(Date& dt)const;
int operator>=(Date& dt)const;
};
int Date::operator==(Date& dt) const
{
return ((this->month == dt.month) && (this->day == dt.day) && (this->year == dt.year));
}
int Date::operator!=(Date& dt)const
{
return !(*this == dt);//一起重载,一种关系运算符取操作另外一种关系运算符
//不等于转去调用==
}
int Date::operator<(Date& dt) const
{
if (this->year == dt.year)
{
if (this->month == dt.month)
return this->day < dt.day;
return this->month < dt.month;
}
return this->year < dt.year;
}
int main()
{
Date d1(2, 14, 2015);
Date d2(6, 19, 2015);
Date d3(2, 14, 2015);
if (d1 < d2)
{
cout << "d1小于d2" << endl;
}
if (d1 == d3)
{
cout << "OK" << endl;
}
cout << "asdhu adas d" << endl;
system("pause");
return 0;
}
操作符重载
最新推荐文章于 2020-02-13 22:07:23 发布