在昨天my_string的基础上,将能重载的运算符全部重载掉
关系运算符:>、<、==、>=、<=、!=
加号运算符:+
取成员运算符:[]
赋值运算符: =
#include <iostream>
using namespace std;
class Stu
{
public:
Stu(){}
Stu(int a,int s):age(a),score(s){}
friend Stu operator+(Stu& s1,Stu& s2);
friend bool operator>(Stu& s1,Stu& s2);
friend bool operator<(Stu& s1,Stu& s2);
friend bool operator==(Stu& s1,Stu& s2);
friend bool operator>=(Stu& s1,Stu& s2);
friend bool operator<=(Stu& s1,Stu& s2);
friend bool operator!=(Stu& s1,Stu& s2);
Stu& operator=(const Stu& s1)
{
this->age = s1.age;
this->score = s1.score;
}
void show()
{
cout<<age<<" : "<<score<<endl;
}
private:
int age,score;
};
Stu operator+(Stu& s1,Stu& s2)
{
Stu tem;
tem.age = s1.age+s2.age;
tem.score=s1.score+s2.score;
return tem;
}
bool operator>(Stu& s1,Stu& s2)
{
if(s1.age>s2.age&&s1.score>s2.score)
return true;
else
return false;
}
bool operator<(Stu& s1,Stu& s2)
{
if(s1.age<s2.age&&s1.score<s2.score)
return true;
else
return false;
}
bool operator==(Stu& s1,Stu& s2)
{
if(s1.age==s2.age&&s1.score==s2.score)
return true;
else
return false;
}
bool operator>=(Stu& s1,Stu& s2)
{
if(s1.age>=s2.age&&s1.score>=s2.score)
return true;
else
return false;
}
bool operator<=(Stu& s1,Stu& s2)
{
if(s1.age<=s2.age&&s1.score<=s2.score)
return true;
else
return false;
}
bool operator!=(Stu& s1,Stu& s2)
{
if(s1.age!=s2.age&&s1.score!=s2.score)
return true;
else
return false;
}
int main()
{
Stu s1(1,2);
s1.show();
Stu s2(3,4);
Stu s3;
s3 = s1+s2;
s3.show();
Stu s4;
s4 = s3;
s4.show();
int a = s1>s2;
int b = s1<s2;
int c = s4==s3;
int d = s1>=s2;
int e = s1<=s2;
int f = s4!=s3;
cout<<a<<endl;
cout<<b<<endl;
cout<<c<<endl;
cout<<d<<endl;
cout<<e<<endl;
cout<<f<<endl;
cout << "Hello World!" << endl;
return 0;
}