c++中运算符重载,+,-,--,+=,-=,*,/,*=,/=,
来源:互联网 发布:免费注册域名送空间 编辑:程序博客网 时间:2024/05/17 21:05
#include<iostream>#include<stdlib.h>using namespace std; class Complex{public: Complex(float r=0,float i =0):_r(r),_i(i){}void print() const{cout<<" _r "<<_r<<" _i "<<_i<<endl;}private:float _r;float _i;public:const Complex& operator+=(const Complex & y);const Complex& operator-=(const Complex & y);const Complex& operator*=(const Complex & y);const Complex& operator/=(const Complex & y);const Complex operator-(const Complex & y);const Complex operator+(const Complex & y);const Complex Complex::operator--();const bool operator==(const Complex & y);const bool operator!=(const Complex & y);};inline const Complex Complex::operator-(const Complex & y){Complex c;c._r=_r-y._r;c._i=_i-y._i;return c;}inline const Complex Complex::operator--(){_r=_r-1;_i=_i-1;return *this;}inline const bool Complex::operator==(const Complex & y){bool b=true;if((*this)._r!=y._r||(*this)._i!=y._i)b=false;return b;}inline const bool Complex::operator!=(const Complex & y){bool b=true;if((*this)._r==y._r&&(*this)._i==y._i)b=false;return b;}inline const Complex Complex::operator+(const Complex & y){Complex c;c._r=_r+y._r;c._i=_i+y._i;return c;}inline const Complex& Complex::operator+=(const Complex & y){_r=_r+y._r;_i=_i+y._i;return *this;}inline const Complex& Complex::operator-=(const Complex & y){*this=*this-y; return *this;}inline const Complex& Complex::operator*=(const Complex & y){_r=_r*y._r-_i*y._i;_i=_r*y._i+_i*y._r;return *this;}inline const Complex& Complex::operator/=(const Complex & y){if(y._r==0 && y._i==0){exit(1);}float den=_r*y._r+_i*y._i;_r=(_r*y._r+_i*y._i)/den;_i=(_i*y._r-_r*y._i)/den; return *this;}int main(){Complex x(2,3),y(-1,3);cout<<" x is ";x.print();cout<<" y is ";y.print();(x+=y).print();x.operator+=(y).print();(x-=y).print();x.operator-=(y).print();(x*=y).print();x.operator*=(y).print();(x/=y).print();x.operator/=(y).print();cout<<(x==y)<<endl;cout<<(x!=y)<<endl;return 0;}
- C#==>运算符重载
- 重载=运算符
- +=运算符重载
- 重载“=”运算符
- 重载运算符=
- 十九、运算符重载(三)String类的改进实现、[]运算符重载、+运算符重载、+=运算符重载、<<运算符重载、>>运算符重载
- C++中几个特殊运算符的重载(= 、++、--、下标运算符的重载)
- == 运算符重载
- C++ 运算符重载 =
- 关于=运算符重载
- 重载赋值运算符=
- C++运算符重载+、-、=
- c++中运算符重载,+,-,--,+=,-=,*,/,*=,/=,
- c++中拷贝构造函数与“=运算符重载”
- C++中深浅拷贝问题以及重载=运算符
- 重载运算符 == C#
- Python ==运算符重载
- vector<tmplate> == 运算符重载
- OpenGL进阶(十一) - GLSL4.x中的数据传递
- C# 中SocketShutdown的意思
- http cookie
- Trim函数的用法
- 07-倒数计时跳转页面
- c++中运算符重载,+,-,--,+=,-=,*,/,*=,/=,
- hdu 4648 - Magic Pen 6(“水”题)
- 08-header禁用缓存
- dreamweaverCS6整合eclipse开发之android本地应用
- 分析TCP/IP协议栈代码之UDP(STM32平台)
- java 加密技术
- Unix原理与应用(第四版)学习笔记1--理论篇
- 笑话一个
- 09-php文件下载