c++ day4
#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;
}