- #include
-
- using namespace std;
-
- class Complex
- {
- private:
- int real; //实部
- int vir; //虚部
-
-
- public:
- Complex() {}
- Complex(int r, int v):real(r), vir(v) {} //有参构造
-
-
- //定义展示函数
- void show()
- {
- if(vir>=0)
- {
- cout<
" + "<"i"< - }else
- {
- cout<
"i"< - }
- }
-
-
- //全局函数版实现加运算符重载
- friend const Complex operator+ (const Complex &L, const Complex &R);
-
-
- //成员函数版实现运算符重载
- const Complex operator- ( const Complex &R)const
- {
- Complex c;
-
-
- c.real = this->real - R.real;
- c.vir = this->vir - R.vir;
-
-
- return c;
- }
-
- //成员函数版实现关系运算符的重载:实部>实部 && 虚部>虚部
- bool operator>(const Complex &R)const
- {
- return this->real>R.real&&this->vir>R.vir;
- }
-
- //重载中括号运算符
- int & operator[](int index)
- {
- if(index == 0)
- {
- return real; //返回实部
- }else if(index == 1)
- {
- return vir; //返回虚部
- }
- }
-
- //重载+=运算符:实部+=实部 虚部+=虚部
- Complex & operator+=(const Complex &R)
- {
- this->real += R.real;
- this->vir += R.vir;
-
- return *this; //返回自身的引用
- }
-
- //重载负号运算符: 实部= -实部, 虚部 = -虚部
- Complex operator-()
- {
- Complex c;
- c.real = -this->real;
- c.vir = -this->vir;
-
- return c;
- }
-
- //重载前置自增运算符重载函数:实部 = 实部+1 虚部=虚部+1
- Complex &operator++()
- {
- ++this->real;
- ++this->vir;
-
- return *this;
- }
-
- //重载后置自增运算符重载函数:实部 = 实部+1 虚部=虚部+1
- Complex operator++(int)
- {
- Complex c;
-
- c.real = this->real++;
- c.vir = this->vir++;
-
- return c;
- }
-
- //将全局函数版实现的输出运算符重载函数设置成友元
- friend ostream &operator<<(ostream &L, Complex &c);
-
- //重载小括号运算符,做一个仿函数
- void operator()(string s)
- {
- cout<
- }
-
- //重载小括号运算符,做一个仿函数
- void operator()()
- {
- cout<<520<<" "<<1314<
- }
-
- //重写类型转换运算符
- operator int()
- {
- return real; //将该数据类型向int类型转换后,使用的是real的值
- }
-
- };
-
- //全局函数版实现加号运算符重载:实部+实部 虚部+虚部
- const Complex operator+ (const Complex &L, const Complex &R)
- {
- //定义一个临时空间
- Complex c;
-
-
- c.real = L.real + R.real;
- c.vir = L.vir + R.vir;
-
-
- return c;
- }
-
-
- //重载输出运算符函数
- ostream &operator<<(ostream &L, Complex &c)
- {
- if(c.vir>=0)
- {
- L<
" + "<"i"< - }else
- {
- L<
"i"< - }
-
- //返回左操作数自身的引用
- return L;
- }
-
- int main()
- {
- Complex c1(5,3);
- c1.show(); //5+3i
-
- Complex c2(2,-1);
- c2.show(); //2-1i
-
- Complex c3 = c1-c2; //调用加法运算符重载函数 c1.operator-(c2)
-
- c3.show(); //3+4i
-
- if(c3 > c2) //调用关系运算符重载函数
- {
- cout<<"yes"<
- }else
- {
- cout<<"no"<
- }
-
- c3[0] = 5; //将实部进行修改成5,调用中括号运算符重载
- c3.show(); //5+4i
-
- c3 += c2; //调用+=运算符重载函数
- c3.show(); //7+3i
-
- Complex c4 = -c3; //调用-号运算符重载
- c4.show(); //-7 - 3i
- c3.show(); //7+3i
-
- Complex c5 = ++c3; //调用前置自增运算符重载函数
- c5.show(); //8+4i
- c3.show(); //8+4i
-
- Complex c6 = c3++; //调用后置自增运算符重载函数
- c6.show(); //8+4i
- c3.show(); //9+5i
-
- cout<
//cout.operator<<(c3) -
- c3("hello world"); //c3.operator()("hello world");
- c3();
-
- int num = (int)c3; //调用类型转换运算符重载函数
- cout<<"num = "<
- return 0;
- }
-
相关阅读:
Gopsutil/Process常用进程监控资源信息
npm WARN npm npm does not support Node.js v12.18.3
【数据结构-进阶】二叉搜索树
行为型模式-策略模式
判断一个数是否偶数(深度思考)
在模块中使用外部依赖的类
汽车托运汽车会产生公里数吗?
python Calendar日历模块函数介绍
基于Springboot+Vue的社区医院管理系统
数据结构——二叉搜索树的实现、删除(最大值和最小值、最大值和最小值)
-
原文地址:https://blog.csdn.net/weixin_69452640/article/details/133561185