赞
踩
算数运算符
- #include <iostream>
- using namespace std;
- class Num
- {
- int rel; //实部
- int vir; //虚部
- public:
- Num():rel(2),vir(1){}
- Num(int rel,int vir):rel(rel),vir(vir){}
- Num &operator=(const Num &other)
- {
- cout << "Num的拷贝赋值函数" << endl;
- this->rel = other.rel;
- this->vir = other.vir;
- return *this;
- }
- friend Num operator+(const Num n1,const Num n2);
- friend Num operator-(const Num n1,const Num n2);
- friend Num operator*(const Num n1,const Num n2);
- friend Num operator/(const Num n1,const Num n2);
- friend Num operator%(const Num n1,const Num n2);
-
- Num operator+(const Num n1);
- Num operator-(const Num n1);
- Num operator*(const Num n1);
- Num operator/(const Num n1);
- Num operator%(const Num n1);
- void show();
- };
-
- Num Num::operator+(const Num n1 )
- {
- Num temp;
- temp.rel=this->rel+n1.rel;
- temp.vir=this->vir+n1.vir;
- return temp;
- }
- Num Num::operator-(const Num n1)
- {
- Num temp;
- temp.rel=this->rel-n1.rel;
- temp.vir=this->vir-n1.vir;
- return temp;
- }
- Num Num::operator*(const Num n1)
- {
- Num temp;
- temp.rel=this->rel*n1.rel;
- temp.vir=this->vir*n1.vir;
- return temp;
- }
- Num Num::operator/(const Num n1)
- {
- Num temp;
- temp.rel=this->rel/n1.rel;
- temp.vir=this->vir/n1.vir;
- return temp;
- }
- Num Num::operator%(const Num n1)
- {
- Num temp;
- temp.rel=this->rel%n1.rel;
- temp.vir=this->vir%n1.vir;
- return temp;
- }
-
- //全局函数版的加法运算符重载
- Num operator+(const Num n1,const Num n2)
- {
- Num temp;
- temp.rel = n1.rel+n2.rel;
- temp.vir = n1.vir+n2.vir;
- return temp;
- }
- //减法
- Num operator-(const Num n1,const Num n2)
- {
- Num temp;
- temp.rel = n1.rel-n2.rel;
- temp.vir = n1.vir-n2.vir;
- return temp;
- }
- Num operator*(const Num n1,const Num n2)
- {
- Num temp;
- temp.rel = n1.rel*n2.rel;
- temp.vir = n1.vir*n2.vir;
- return temp;
- }
- Num operator/(const Num n1,const Num n2)
- {
- Num temp;
- temp.rel = n1.rel/n2.rel;
- temp.vir = n1.vir/n2.vir;
- return temp;
- }
- Num operator%(const Num n1,const Num n2)
- {
- Num temp;
- temp.rel = n1.rel%n2.rel;
- temp.vir = n1.vir%n2.vir;
- return temp;
- }
- void Num::show()
- {
- cout << rel << "+" << vir << "i" << endl;
- }
- int main()
- {
- Num n1;
- Num n2(1,4);
- Num n3;
- n3 = n1+n2;
- n3.show();
- return 0;
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- #include <iostream>
-
- using namespace std;
- class Num
- {
- int rel; //实部
- int vir; //虚部
- public:
- Num():rel(2),vir(1){}
- Num(int rel,int vir):rel(rel),vir(vir){}
- Num &operator=(const Num &other)
- {
- cout << "Num的拷贝赋值函数" << endl;
- this->rel = other.rel;
- this->vir = other.vir;
- return *this;
- }
- friend bool operator>(const Num n1,const Num n2);
- friend bool operator>=(const Num n1,const Num n2);
- friend bool operator<(const Num n1,const Num n2);
- bool operator<=(const Num n1);
- bool operator==(const Num n1);
- bool operator!=(const Num n1);
- };
- bool operator>(const Num n1,const Num n2)
- {
- if(n1.rel>n2.rel)
- {
- return n1.rel>n2.rel;
- }
- else if(n1.rel==n2.rel)
- {
- return n1.vir>n2.vir;
- }
- return n1.rel>n2.rel;
- }
- bool operator>=(const Num n1,const Num n2)
- {
- if(n1.rel>n2.rel)
- {
- return n1.rel>=n2.rel;
- }
- else if(n1.rel==n2.rel)
- {
- return n1.vir>=n2.vir;
- }
- return n1.rel>=n2.rel;
- }
- bool operator<(const Num n1,const Num n2)
- {
- if(n1.rel<n2.rel)
- {
- return n1.rel<n2.rel;
- }
- else if(n1.rel==n2.rel)
- {
- return n1.vir<n2.vir;
- }
- return n1.rel<n2.rel;
- }
- bool Num::operator<=(const Num n1)
- {
- if(this->rel<n1.rel)
- {
- return this->rel<=n1.rel;
- }
- else if(this->rel==n1.rel)
- {
- return this->vir<=n1.vir;
- }
- return this->rel<=n1.rel;
- }
- bool Num::operator==(const Num n1)
- {
- if(this->rel==n1.rel)
- {
- return this->vir==n1.vir;
- }
- return this->rel==n1.rel;
- }
- bool Num::operator!=(const Num n1)
- {
- if(this->rel!=n1.rel)
- {
- return this->rel!=n1.rel;
- }
- else if(this->rel==n1.rel)
- {
- return this->vir!=n1.vir;
- }
- return this->rel!=n1.rel;
- }
- int main()
- {
-
- return 0;
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- #include <iostream>
-
- using namespace std;
- class Num
- {
- int rel; //实部
- int vir; //虚部
- public:
- Num():rel(2),vir(1){}
- Num(int rel,int vir):rel(rel),vir(vir){}
- Num &operator=(const Num &other)
- {
- cout << "Num的拷贝赋值函数" << endl;
- this->rel = other.rel;
- this->vir = other.vir;
- return *this;
- }
- friend bool operator&&(const Num n1,const Num n2);
- friend bool operator||(const Num n1,const Num n2);
-
- };
- bool operator&&(const Num n1,const Num n2)
- {
- if(n1.rel&&n2.rel)
- {
- return n1.vir&&n2.vir;
- }
- return n1.rel&&n2.rel;
- }
- bool operator||(const Num n1,const Num n2)
- {
- if(n1.rel||n2.rel)
- {
- return n1.vir||n2.vir;
- }
- return n1.rel||n2.rel;
- }
- int main()
- {
- cout << "Hello World!" << endl;
- return 0;
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。