一、關(guān)系運(yùn)算符重載 > >= < <= == !=
#include <iostream>
using namespace std;
class Relates
{
private:
int a;
int b;
public:
Relates() {}
Relates(int a,int b):a(a),b(b) {}
bool operator>(const Relates &R) const
{
if((a>R.a&&b>R.b) || (a==R.a&&b>R.b) || (a>R.a&&b==R.b))
return true;
return false;
}
bool operator>=(const Relates &R) const
{
if(a>=R.a && b>=R.b)
return true;
return false;
}
bool operator<(const Relates &R) const
{
if((a<R.a&&b<R.b) || (a==R.a&&b<R.b) || (a<R.a&&b==R.b))
return true;
return false;
}
bool operator<=(const Relates &R) const
{
if(a<=R.a && b<=R.b)
return true;
return false;
}
bool operator==(const Relates &R) const
{
if(a==R.a && b==R.b)
return true;
return false;
}
};
int main()
{
Relates a1(10,20);
Relates a2(20,20);
if(a1 > a2)
cout << "a1 > a2" << endl;
if(a1 >= a2)
cout << "a1 >= a2" << endl;
if(a1 < a2)
cout << "a1 < a2" << endl;
if(a1 <= a2)
cout << "a1 <= a2" << endl;
if(a1 == a2)
cout << "a1 == a2" << endl;
else
cout << "a1 != a2" << endl;
return 0;
}
【輸出樣例】
a1 < a2
a1 <= a2
a1 != a2
?二、知識(shí)點(diǎn)整理
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-669123.html
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-669123.html
到了這里,關(guān)于C++,運(yùn)算符重載——關(guān)系運(yùn)算符練習(xí)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!