运算符重载
类外函数实现:
#include <iostream>using namespace std;class Good
{//算数friend const Good operator*(const Good &L,const Good &R);friend const Good operator+(const Good &L,const Good &R);friend const Good operator/(const Good &L,const Good &R);friend const Good operator-(const Good &L,const Good &R);friend const Good operator%(const Good &L,const Good &R);//关系friend bool operator>(const Good &L,const Good &R);friend bool operator>=(const Good &L,const Good &R);friend bool operator<(const Good &L,const Good &R);friend bool operator<=(const Good &L,const Good &R);friend bool operator==(const Good &L,const Good &R);//赋值friend Good &operator-=(Good &L,const Good &R);friend Good &operator+=(Good &L,const Good &R);friend Good &operator*=(Good &L,const Good &R);friend Good &operator/=(Good &L,const Good &R);friend Good &operator%=(Good &L,const Good &R);
private:int n;int f;int a;
public:Good(){}Good(int n,int f,int a):n(n),f(f),a(a){}void show() const{cout << "n:" << n << endl;cout << "f:" << f << endl;cout << "a:" << a << endl;//cout << "常" << endl;}};
//非成员
//算数+
const Good operator+(const Good &L,const Good &R)
{Good t;t.a=L.a+R.a;t.n=L.n+R.n;t.f=L.f+R.f;return t;
}
//算数*
const Good operator*(const Good &L,const Good &R)
{Good t;t.a=L.a*R.a;t.n=L.n*R.n;t.f=L.f*R.f;return t;
}
//算数-
const Good operator-(const Good &L,const Good &R)
{Good t;t.a=L.a-R.a;t.n=L.n-R.n;t.f=L.f-R.f;return t;
}
//算数/
const Good operator/(const Good &L,const Good &R)
{Good t;t.a=L.a/R.a;t.n=L.n/R.n;t.f=L.f/R.f;return t;
}
//算数%
const Good operator%(const Good &L,const Good &R)
{Good t;t.a=L.a%R.a;t.n=L.n%R.n;t.f=L.f%R.f;return t;
}
//关系运算<
bool operator<(const Good &L,const Good &R)
{if(L.a<R.a && L.n<R.n && L.f<R.f){return true;}else{return false;}
}
//关系运算>
bool operator>(const Good &L,const Good &R)
{if(L.a>R.a && L.n>R.n && L.f>R.f){return true;}else{return false;}
}
//关系运算>=
bool operator>=(const Good &L,const Good &R)
{if(L.a>=R.a && L.n>=R.n && L.f>=R.f){return true;}else{return false;}
}
//关系运算<=
bool operator<=(const Good &L,const Good &R)
{if(L.a<=R.a && L.n<=R.n && L.f<=R.f){return true;}else{return false;}
}
//关系运算==
bool operator==(const Good &L,const Good &R)
{if(L.a==R.a && L.n==R.n && L.f==R.f){return true;}else{return false;}
}
//赋值运算-=
Good &operator-=(Good &L,const Good &R)
{L.a-=R.a;L.n-=R.n;L.f-=R.f;return L;
}
//赋值运算+=
Good &operator+=(Good &L,const Good &R)
{L.a+=R.a;L.n+=R.n;L.f+=R.f;return L;
}
//赋值运算*=
Good &operator*=(Good &L,const Good &R)
{L.a*=R.a;L.n*=R.n;L.f*=R.f;return L;
}
//赋值运算/=
Good &operator/=(Good &L,const Good &R)
{L.a/=R.a;L.n/=R.n;L.f/=R.f;return L;
}
//赋值运算%=
Good &operator%=(Good &L,const Good &R)
{L.a%=R.a;L.n%=R.n;L.f%=R.f;return L;
}int main()
{cout << "-----0-----" << endl;Good s0(1,2,3);s0.show();cout << "-----1-----" << endl;Good s1(3,2,1);s1.show();cout << "----2.0----" << endl;Good s2=s0+s1;s2.show();cout << "----2.1----" << endl;s2+=s1;s2.show();cout << "s2>s1?" << endl;bool outcome=s2>s1;cout << outcome << endl;return 0;
}
类内函数实现:
#include <iostream>using namespace std;class Good
{
private:int n;int f;int a;
public:Good(){}Good(int n,int f,int a):n(n),f(f),a(a){}void show() const{cout << "n:" << n << endl;cout << "f:" << f << endl;cout << "a:" << a << endl;}//成员//算数+const Good operator+(const Good &p) const{Good t;t.a=a+p.a;t.n=n+p.n;t.f=f+p.f;return t;}//算数-const Good operator-(const Good &p) const{Good t;t.a=a-p.a;t.n=n-p.n;t.f=f-p.f;return t;}//算数*const Good operator*(const Good &p) const{Good t;t.a=a*p.a;t.n=n*p.n;t.f=f*p.f;return t;}//算数/const Good operator/(const Good &p) const{Good t;t.a=a/p.a;t.n=n/p.n;t.f=f/p.f;return t;}//算数%const Good operator%(const Good &p) const{Good t;t.a=a%p.a;t.n=n%p.n;t.f=f%p.f;return t;}//关系>bool operator>(const Good &R) const{if(a>R.a && n>R.n && f>R.f){return true;}else{return false;}}//关系<bool operator<(const Good &R) const{if(a<R.a && n<R.n && f<R.f){return true;}else{return false;}}//关系==bool operator==(const Good &R) const{if(a==R.a && n==R.n && f==R.f){return true;}else{return false;}}//关系>=bool operator>=(const Good &R) const{if(a>=R.a && n>=R.n && f>=R.f){return true;}else{return false;}}//关系<=bool operator<=(const Good &R) const{if(a<=R.a && n<=R.n && f<=R.f){return true;}else{return false;}}//赋值+=Good &operator+=(const Good &R){a+=R.a;n+=R.n;f+=R.f;return *this;}//赋值=Good &operator=(const Good &R){a=R.a;n=R.n;f=R.f;return *this;}//赋值-=Good &operator-=(const Good &R){a-=R.a;n-=R.n;f-=R.f;return *this;}//赋值%=Good &operator%=(const Good &R){a%=R.a;n%=R.n;f%=R.f;return *this;}//赋值/=Good &operator/=(const Good &R){a/=R.a;n/=R.n;f/=R.f;return *this;}//赋值*=Good &operator*=(const Good &R){a*=R.a;n*=R.n;f*=R.f;return *this;}
};int main()
{cout << "-----0-----" << endl;Good s0(1,2,3);s0.show();cout << "-----1-----" << endl;Good s1(3,2,1);s1.show();cout << "----2.0----" << endl;Good s2=s0+s1;s2.show();cout << "----2.1----" << endl;s2+=s1;s2.show();cout << "s2>s1?" << endl;bool outcome=s2>s1;cout << outcome << endl;return 0;
}