C++实现一个复数类
2018-03-23 18:12
429 查看
要求包括:
1.实现复数类的加减乘除
2.实现出类的构造、拷贝构造、析构、重载
1.实现复数类的加减乘除
2.实现出类的构造、拷贝构造、析构、重载
#include<iostream> using namespace std; class Complex { private: double _real;//实部 double _imag;//虚部 public: Complex(double, double);//构造函数 Complex(Complex& c);//拷贝构造函数 ~Complex();//析构函数 void show(); //复数和复数的计算 Complex operator+(Complex& c); Complex operator-(Complex& c); Complex operator*(Complex& c); Complex operator/(Complex& c); //复数和实数的计算 Complex operator+(int& c); Complex operator-(int& c); Complex operator*(int& c); Complex operator/(int& c); }; Complex::Complex(double real = 0, double imag = 0)//构造函数 { _real = real; _imag = imag; } Complex::Complex(Complex& c)//拷贝构造函数 { _real = c._real; _imag = c._imag; } Complex::~Complex()//析构函数 { _real = 0.0; _imag = 0.0; } void Complex::show() { cout << _real << " + " << _imag << " * i"<<endl; } Complex Complex::operator+(Complex& c) { Complex sum; sum._real = this->_real + c._real; sum._imag = this->_imag + c._imag; return sum; } Complex Complex::operator-(Complex& c) { Complex sub; sub._real = _real - c._real; sub._imag = _imag - c._imag; return sub; } Complex Complex::operator*(Complex& c) { //(a+bi)(c+di)=(ac-bd)+(bc+ad)i. Complex mul; mul._real = _real * (c._real) - _imag * (c._imag); mul._imag = _imag * (c._real) + _real * (c._imag); return mul; } Complex Complex::operator/(Complex& c) { //(a+bi)/(c+di)=(ac+bd)/(c^2+d^2) +((bc-ad)/(c^2+d^2))i Complex div; div._real = (_real*c._real + _imag * c._imag) / (c._real*c._real + c._imag*c._imag); div._imag = (_imag*c._real - _real * c._imag) / (c._real*c._real + c._imag*c._imag); return div; } Complex Complex::operator+(int& c) { Complex sum; sum._real = _real + c; sum._imag = _imag; return sum; } Complex Complex::operator-(int& c) { Complex sub; sub._real = _real - c; sub._imag = _imag; return sub; } Complex Complex::operator*(int& c) { Complex mul; mul._real = _real * c; mul._imag = _imag; return mul; } Complex Complex::operator/(int& c) { Complex div; div._real = _real / c; div._imag = _imag; return div; } int main() { Complex c1; Complex c2(2, 3); Complex c3(7, 8); Complex c4(3, 4); Complex out; cout << " 复数1: "; c1.show(); cout << " 复数2: "; c2.show(); cout << " 复数3: "; c3.show(); cout << " 复数4: "; c4.show(); cout << "复数1,2相加: "; out = c1 + c2; out.show(); cout << "复数3,4相减: "; out = c3 - c4; out.show(); cout << "复数2,3相乘: "; out = c2 * c3; out.show(); cout << "复数2,4相除: "; out = c2 / c4; out.show(); system("pause"); return 0; }
相关文章推荐
- 【C++】模拟实现一个复数类,要求实现 加,减,乘,除等基本运算符的重载
- C++ 声明并实现一个复数类
- 【C++】用拷贝函数实现一个复数类(complex)
- 【C++】实现一个复数类(complex)(带有默认参数的构造函数 )
- C++实现一个复数类
- 求一个区间内所有的质数(C++实现)
- 用C++实现一个Log系统
- 用C++编写函数判别一个数是否是质数,在主程序中实现输入输出
- 采用C++的ACE库实现的一个通用的C/S架构通信程序(最终版)
- 三种实现交换的方法,其中一个诡异的swap函数——C/C++语言(问题求助)
- 从信息隐藏的一个需求看C++接口与实现的分离
- 一个C++的粒子群(PSO)算法实现
- 用C++实现一个Brainfuck解释器
- 设计一个不能被继承的类-C++实现(来源于剑指名企offer)
- 一个linux下简单的纯C++实现Http请求类(GET,POST,上传,下载)
- [转]如何用C++实现一个LRU Cache
- C++中用两栈实现一个队列
- Hypertable一个 C++ 的Bigtable开源实现
- C++实现的一个哈希表类
- C++实现一个string类