【C++】复数类
来源:互联网 发布:js中get post 区别 编辑:程序博客网 时间:2024/05/17 09:06
#include<iostream>using namespace std;class Complex{public:void Display(){cout<<"Real:"<<_real<<"Image:"<<_image<<endl;}// 带缺省值的构造函数Complex (double real = 0.0, double image = 0.0):_real(real),_image(image){//初始化列表cout<<"Complex (double real = 0.0, double image = 0.0)"<<endl;}// 析构函数~Complex(){cout<<"~Complex()"<<endl;}// 拷贝构造函数Complex (const Complex& d):_image(d._image),_real(d._real){//初始化列表cout<<"Complex (const Complex& d)"<<endl;}// 赋值运算符重载Complex& operator= (const Complex& d){cout<<"operator= (const Complex& d)"<<endl;if (this != &d){this->_real = d._real;this->_image = d._image;}return *this;}Complex operator +(Complex& c)//声明成员函数重载运算符"+"{cout<<"operator +(Complex& c)"<<endl;Complex temp;temp._real = this->_real + c._real;temp._image = this->_image + c._image;return temp;}Complex operator -(Complex& c){cout<<"operator -(Complex& c)"<<endl;Complex temp;temp._real = this->_real - c._real;temp._image = this->_image - c._image;return temp;}Complex operator *(Complex& c){cout<<"operator *(Complex& c)"<<endl;Complex temp;temp._real = this->_real*c._real - this->_image * c._image;temp._image = this->_real *c._image + this->_image * c._real;return temp;}Complex operator /(Complex& c){cout<<"operator -(Complex& c)"<<endl;Complex temp;temp._real = (this->_real*c._real + this->_image * c._image)/(c._real*c._real + c._image * c._image);temp._image = (this->_image * c._real -this->_real *c._image)/(c._real*c._real + c._image * c._image);return temp;}Complex& operator++()//前置++{this->_real++;return *this;}Complex operator++(int) //后置++{Complex temp(*this);this->_real++;return temp;}Complex& operator--()//前置--{this->_real--;return *this;}Complex operator--(int) //后置--{Complex temp(*this);this->_real--;return temp;}Complex& operator-=(const Complex& c){this->_real -= c._real;this->_image -= c._image;return *this;}Complex& operator+=(const Complex& c){this->_real += c._real; //(this->_real = this->_real + c._real)this->_image += c._image;return *this;}private:double _real;double _image;};void TestComplex (){Complex c1(2.2, 1.1);Complex c2 = c1++;c1.Display();c2.Display();}int main(){Complex c1(2.2,1.1);c1.Display ();Complex c2(c1);//调用拷贝构造函数c2.Display();Complex c3;//赋值运算符的重载c3 = c2;c3.Display();Complex c4 = c1 + c2;c4.Display();Complex c5 = c3 - c2;c5.Display();Complex c6 = c3 * c2;c6.Display();Complex c7(c1);c7++;c7.Display();Complex c8;c8 += c1;c8.Display();getchar();return 0;}
0 0
- 【c/c++】复数类
- 【c++】复数加减法类
- 【C++】复数类
- 【C++】复数类
- 【C++】 复数类操作
- 【C++】实现复数类
- [C#]复数类,练习重载
- 【c++】实现一个复数类
- [c++]复数类的实现
- 【C++】复数类的实现
- 【C++】c++复数类Complex
- 【C++】复数类的实现
- 【C++】复数类的实现
- 复数类
- 复数类
- 复数类
- 复数类
- 复数类
- hdoj-2066 一个人的旅行【最短路径--dijkstra&&spfa&&floyd】
- C# LinkedList<T> 泛型类的实现
- FZU 2135 数字游戏
- 多线程 - 07.GCD函数和队列组合示例
- The hardest problem
- 【C++】复数类
- 软件架构的道德责任
- 动画
- 常用传感器协议6:中立格林VOC传感器
- BUG与异常小结
- uva 10652 - Board Wrapping(凸包)
- ssh常见错误
- 解决XCODE配置LLVM环境出现的问题
- 黑马程序员-oc-分类(Q)