类外函数实现:
- #include
-
- 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
- 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
-
- 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
- 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;
- }
-
相关阅读:
docker-compose 部署rabbitmq 15672打不开
C# 使用 RSA 加密算法生成证书签名产生“The system cannot find the file specified”异常
Linux学习记录——이십팔 网络基础(1)
在Mac上使用安卓桌面模式
9.14-广读最新研究方向论文核心思路汇总
数据库顶会 VLDB 2023 论文解读:字节跳动如何解决超大规模流式任务运维难题
基于springboot的高校失物招领系统毕业设计源码111731
Windows系统搭建VisualSVN服务并结合内网穿透实现公网访问
群晖-使用docker套件部署Prometheus+Grafana
Scrapy + selenium + 超级鹰验证码识别爬取网站
-
原文地址:https://blog.csdn.net/m0_61834469/article/details/133754410