C++完成复数类的运算符重载
来源:互联网 发布:淘宝化妆品店铺简介 编辑:程序博客网 时间:2024/06/09 01:57
复数类的运算符重载
运算符重载指对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型
对于复数类,有实部和虚部两部分,因此我们对于+,-,*,/,要列式计算。
如要加上+=,-+,*=,/=也会很容易。
程序如下:
#include<iostream>using namespace std;class Complex{public:Complex(double real = 0.0, double image = 0.0); //声明构造函数时指定默认参数Complex& operator=(const Complex& c);Complex operator+(const Complex& c);Complex operator-(const Complex& c);Complex operator*(const Complex& c);Complex operator/(const Complex& c);void display();private:double _real;double _image;};//定义构造函数Complex::Complex(double real, double image){_real = real;_image = image;}//复数赋值Complex& Complex::operator=(const Complex& c){if (this != &c){_real = c._real;_image = c._image;}return *this;}//复数相加Complex Complex::operator+(const Complex& c){Complex c1;c1._real = _real + c._real;c1._image = _image + c._image;return c1;}//复数相减Complex Complex::operator-(const Complex& c){Complex c1;c1._real = _real - c._real;c1._image = _image - c._image;return c1;}//复数相乘(a+bi)*(c+di)Complex Complex::operator*(const Complex& c){Complex c1;c1._real = _real*c._real - _image*c._image;c1._image = _image*c._real + _real*c._image;return c1;}//复数相除(a+bi)/(c+di)Complex Complex::operator/(const Complex& c){Complex c1;c1._real = (_real*c._real + _image*c._image) / (c._real*c._real + c._image*c._image);c1._image = (_image*c._real - _real*c._image) / (c._real*c._real + c._image*c._image);return c1;}void Complex::display(){cout << "(" << _real << "," << _image << ")" << endl;}void test(){Complex c1(1.0, 2.0);Complex c2(4.0, 5.0);Complex c3;c3 = c1.operator+(c2);c3.display();c3 = c1.operator-(c2);c3.display();c3 = c1.operator*(c2);c3.display();c3 = c1.operator/(c2);c3.display();c2 = c1.operator=(c1);c2.display();}int main(){test();system("pause");return 0;}
在此基础上,就可以直接利用已重载好的运算符来重载+=,-=,*=,/=
程序如下:
//复数加等(a+bi)=(a+bi)+(c+di)void Complex:: operator+=(const Complex& c){*this=*this+c;/*_real += c._real;_image += c._image;*/}//复数减等(a+bi)=(a+bi)-(c+di)void Complex:: operator-=(const Complex& c){*this = *this - c;/*_real -= c._real;_image -= c._image;*/}//复数乘等(a+bi)=(a+bi)*(c+di)void Complex:: operator*=(const Complex& c){*this = *this*c;}//复数除等(a+bi)=(a+bi)/(c+di)void Complex::operator/=(const Complex& c){*this = *this / c;}void Complex::display(){cout << "(" << _real << "," << _image << ")" << endl;}void test2(){Complex c1(1.0, 2.0);Complex c2(4.0, 5.0);c1+=c2;c1.display();c1-=(c2);c1.display();c1*=(c2);c1.display();c1/=(c2);c1.display();}
运行结果如下,读者可自行验证。
阅读全文
0 0
- C++完成复数类的运算符重载
- [c++]复数的运算符重载
- 【C++】复数的运算符重载
- 【项目1】实现复数类中的运算符重载--用类的友元函数,完成运算符的重载;
- 第八周 项目1--用类的友元函数完成复数类重载运算符
- 使用友元函数来完成复数类的运算符重载
- 实现复数类的运算符重载
- 复数类中运算符的重载
- 复数类的运算符重载
- 实现复数类的运算符重载
- 复数类加减乘除的运算符重载
- C++复数类的运算符重载
- 复数类的运算符重载
- C++复数类的运算符重载
- 复数类的运算符重载
- 复数类-简单的运算符重载
- 复数类的运算符重载
- 复数运算符的重载
- Java反射基础
- matlab simulink保存出错的问题
- 欢迎使用CSDN-markdown编辑器
- 指针常量,常量指针,指针 区别
- Ubuntu 需要安装软件地址记录
- C++完成复数类的运算符重载
- 循环的套路
- POJ 3694 Network (缩点 + 求割边 + 暴力LCA)
- 强连通图和双连通图
- 数据仓库与数据库的区别
- swiper 关于h5页面无法触发A标签问题
- 译:Opencv: cv::DataType<_Tp>文档
- 对PCA降维的理解
- 2728:摘花生(2.6基本算法之动态规划)