一、运算符重载简介
C++为了增强代码的可读性,引入了运算符重载。
运算符重载是一个拥有特殊函数名的函数,其函数名为 operator+重载的运算符,其余的返回值类型和参数列表与普通函数类似。调用时可以使用函数名调用,也可以直接使用重载的运算符来调用。
如下代码,d1 == d2会被编译器替换为operator==(d1, d2);d1 == d2会被编译器替换为d1.operator==(d2)
运算符重载注意事项:
1.不能用operator连接其他符号来创建新的运算符,如operator@(只能是C/C++中已存在的运算符)
2.不能改变用于内置类型的运算符含义
3.以下五个运算符不能重载:
.(类成员访问运算符)
.*(类成员指针访问运算符)
::(域运算符)
sizeof(长度运算符)
? :(条件运算符)
二、重载日期类中的运算符 == < <= > >=
1.全局的运算符重载(全局运算符重载时,类中的成员变量需要改为公有的,否则需要提供Get接口或者利用友元解决,此处以成员变量为公有的为例)
class Date {
public:
int _year = 1;
int _month = 1;
int _day = 1;
public:
//有参构造函数
Date(int year = 1, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
};
//Date类==重载
bool operator==(const Date& d1, const Date& d2)
{
return d1._year == d2._year
&& d1._month == d2._month
&& d1._day == d2._day;
}
//Date类<重载
bool operator<(const Date& d1, const 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;
}
}
}
return false;
}
//Date类>重载
bool operator>(const Date& d1, const Date& d2)
{
return !((d1 == d2) || (d1 < d2));
}
//Date类<=重载
bool operator<=(const Date& d1, const Date& d2)
{
return (d1 < d2) || (d1 == d2);
}
//Date类>=重载
bool operator>=(const Date& d1, const Date& d2)
{
return (d1 > d2) || (d1 == d2);
}
int main()
{
Date d1(2024, 2, 17);
Date d2(2024, 2, 16);
cout << operator==(d1, d2) << endl;//0
cout << (d1 == d2) << endl;//0
cout << operator<(d1, d2) << endl;//0
cout << (d1 < d2) << endl;//0
cout << operator>(d1, d2) << endl;//1
cout << (d1 > d2) << endl;//1
cout << operator>=(d1, d2) << endl;//1
cout << (d1 >= d2) << endl;//1
cout << operator<=(d1, d2) << endl;//0
cout << (d1 <= d2) << endl;//0
return 0;
}
2.运算符重载为成员函数
class Date {
public:
int _year = 1;
int _month = 1;
int _day = 1;
public:
//有参构造函数
Date(int year = 1, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
//Date类==重载
bool operator==(const Date& d)
{
return this->_year == d._year
&& this->_month == d._month
&& this->_day == d._day;
}
//Date类<重载
bool operator<(const Date& d)
{
if (this->_year < d._year)
{
return true;
}
else if (this->_year == d._year)
{
if (this->_month < d._month)
{
return true;
}
else if (this->_month == d._month)
{
if (this->_day < this->_day)
{
return true;
}
}
}
return false;
}
//Date类>重载
bool operator>(const Date& d)
{
return !((*(this) < d) || (*(this) == d));
}
//Date类<=重载
bool operator<=(const Date& d)
{
return ((*this) < d) || (*(this) == d);
}
//Date类>=重载
bool operator>=(const Date& d)
{
return (*(this) > d) || (*(this) == d);
}
};
int main()
{
Date d1(2024, 2, 17);
Date d2(2024, 2, 16);
cout << d1.operator==(d2) << endl;//0
cout << (d1 == d2) << endl;//0
cout << d1.operator<(d2) << endl;//0
cout << (d1 < d2) << endl;//0
cout << d1.operator<=(d2) << endl;//0
cout << (d1 <= d2) << endl;//0
cout << d1.operator>(d2) << endl;//1
cout << (d1 > d2) << endl;//1
cout << d1.operator>=(d2) << endl;//1
cout << (d1 >= d2) << endl;//1
return 0;
}