通过运算符重载实现复数运算
2015-08-19 16:26
232 查看
#include<iostream> using namespace std; class Complex { public: // 带缺省值的构造函数 Complex(double real = 0, double image = 0) { cout << "带缺省值的构造函数" << endl; _real = real; _image = image; } // 析构函数 ~Complex() { cout << "析构函数" << endl; } // 拷贝构造函数 Complex(const Complex& d) { cout << "拷贝构造函数" << endl; _real = d._real; _image = d._image; } // 赋值运算符重载 Complex& operator= (const Complex& d) { cout << "赋值运算符重载" << endl; if (&d != this) { _real = d._real; _image = d._image; } return *this; } void Display() { cout << "real = " << _real << " " << "image = " << _image << endl; } public: Complex& operator++()//前置++ { _real++; return *this; } Complex operator++(int)//后置++ int为标示符区分前置++和后置++ { Complex tmp(*this); _real++; return tmp; } Complex& operator--()//前置-- { _real--; return *this; } Complex operator--(int)//后置-- { Complex tmp(*this); _real--; return tmp; } Complex operator+(const Complex& c) { Complex tmp; tmp._real = _real + c._real; tmp._image = _image + c._image; return tmp; } Complex operator-(const Complex& c) { Complex tmp; tmp._real = this->_real - c._real; tmp._image = this->_image - c._image; return tmp; } Complex& operator-=(const Complex& c) { _real -= c._real; _image -= c._image; return *this; } Complex& operator+=(const Complex& c) { _real += c._real; _image += c._image; return *this; } Complex operator*(const Complex& c) { Complex tmp; tmp._real = (_real * c._real) - (_image * c._image); tmp._image = (_real * c._image) + (_image * c._real); return tmp; } Complex operator/(const Complex& c) { Complex tmp; double t = (c._real * c._real) + (c._image * c._image); tmp._real = (_real * c._real - _image * c._image) / t; tmp._image = (_real * c._image + _image * c._real) / t; return tmp; } private: double _real; // 实部 double _image; // 虚部 }; void TestMultiply() { Complex c1(3.3, 1.1), c2(1.1, 2.2); Complex c3; c3 = c1 * c2; //(1.21,8.47) c3.Display(); } void TestDivide() { Complex c1(3.3, 1.1), c2(1.1, 2.2); Complex c3; c3 = c1 / c2; //(0.2,1.4) c3.Display(); } void TestAdd_Reduce() { Complex c1(4.3, 2.1), c2(1.5, 6.5), c3; c3 = c2 + c1; c3.Display();//(5.8,8.6) c3 = c2 - c1;//(2.2,1.1) c3.Display(); } int main() { Complex c1(2.2, 1.1); Complex c2(c1);//代入法拷贝构造 c1.Display(); c2.Display(); Complex c3 = c1;//赋值法拷贝构造 c3.Display(); Complex c4(3.3, 1.1); c1 = c4;//赋值运算符重载 c1.Display(); ++c4;//(4.3,1.1) c4.Display(); --c4;//(3.3,1.1) c4.Display(); c3 = c4++; c3.Display();//(3.3,1.1) c4.Display();//(4.3,1.1) c4 -= c1;// (1,0) c4.Display(); c4 += c1;//(4.3,1.1) c4.Display(); TestAdd_Reduce(); TestMultiply(); TestDivide(); getchar(); return 0; }
相关文章推荐
- CCF 201312-1出现次数最多的数
- HDU - 4857 逃生(反向建图 + 拓扑排序)
- shell的追踪与调试选项
- 二分图-最大匹配,最小路径覆盖,最小点覆盖(KM算法)
- SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- EL字符串表达式和常用功能用途拦截
- comparable与comparator比较
- fckeditor的用法
- Java jvisualvm简要说明
- 桌面支持--PLM软件必须右键用管理员账号打开
- centos7 lamp环境配置及多域名设置
- 网页浮动图片的设置
- IE获取文件目录下的文件列表及ActiveXObject IE设置
- Perforce初体验
- 桌面支持--Office2013没有Office Picture Manage怎么安装
- Objective-C中不同方式实现锁(一)
- js 判断文件是否存在
- 单线程代码事例
- 多校第九场Too Simple题解
- 13. CSS 链接