#include <iostream>
using namespace std;
class stu
{
friend const bool operator>(const stu &s1,const stu &s2);
friend const bool operator<(const stu &s1,const stu &s2);
friend const bool operator>=(const stu &s1,const stu &s2);
friend const bool operator<=(const stu &s1,const stu &s2);
friend const bool operator==(const stu &s1,const stu &s2);
int a;
int b;
public:
stu(int a,int b):a(a),b(b)
{
cout << "有参构造" << endl;
}
};
const bool operator>(const stu &s1,const stu &s2)//类外+号运算符重载 本质:s3=operator +(s1+s2)
{
if(s1.a>s2.a&&s1.b>s2.b)
{
return true;
}
else
{
return false;
}
}
const bool operator<(const stu &s1,const stu &s2)//类外+号运算符重载 本质:s3=operator +(s1+s2)
{
if(s1.a<s2.a&&s1.b<s2.b)
{
return true;
}
else
{
return false;
}
}
const bool operator>=(const stu &s1,const stu &s2)//类外+号运算符重载 本质:s3=operator +(s1+s2)
{
if(s1.a>=s2.a&&s1.b>=s2.b)
{
return true;
}
else
{
return false;
}
}
const bool operator<=(const stu &s1,const stu &s2)//类外+号运算符重载 本质:s3=operator +(s1+s2)
{
if(s1.a<=s2.a&&s1.b<=s2.b)
{
return true;
}
else
{
return false;
}
}
const bool operator==(const stu &s1,const stu &s2)//类外+号运算符重载 本质:s3=operator +(s1+s2)
{
if(s1.a==s2.a&&s1.b==s2.b)
{
return true;
}
else
{
return false;
}
}
int main()
{
stu s1(20,30);
stu s2(20,20);
if(s1>s2)
cout << "s1>s2" << endl;
else if(s1<s2)
cout << "s1<s2" << endl;
else if(s1==s2)
cout << "s1==s2" << endl;
else if(s1>=s2)
cout << "s1>=s2" << endl;
else if(s1<=s2)
cout << "s1<=s2" << endl;
else
cout << "s1!=s2" << endl;
return 0;
}
文章来源地址https://www.toymoban.com/news/detail-669802.html文章来源:https://www.toymoban.com/news/detail-669802.html
到了这里,关于c++ day4的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!