C++实现的复数运算符重载
2012-11-03 20:22
323 查看
//#include<iostream> //using namespace std; #include <iostream.h> class Complex { private: double real; double img; public: Complex(double lp=0.0,double rp=0.0);//构造函数 Complex(const Complex &rth);//拷贝构造函数 Complex &operator =(const Complex& rh);//赋值运算符 void Print() { cout<<real<<"+"<<img<<"i"<<endl; } friend Complex operator+(const Complex& lp,const Complex& rp); friend Complex operator-(const Complex& lp,const Complex& rp); friend Complex operator*(const Complex& lp,const Complex& rp); }; Complex ::Complex(double t_real,double t_img) { real=t_real; img=t_img; } Complex::Complex(const Complex &rth) { *this=rth; } Complex& Complex::operator=(const Complex& rth) { if (this==&rth) return *this; else { real=rth.real; img=rth.img; return *this; } } Complex operator+(const Complex& cp1,const Complex& cp2) { return Complex(cp1.real+cp2.real,cp1.img+cp2.img); } Complex operator-(const Complex& cp1,const Complex& cp2) { return Complex(cp1.real-cp2.real,cp1.img-cp2.img); } Complex operator*(const Complex&cp1,const Complex&cp2) { return Complex (cp1.real*cp2.real-cp1.img*cp2.img,cp1.real*cp2.img+cp2.real*cp1.img); } int main () { Complex lp(2,3); Complex rp(1,1); Complex res; Complex cb(lp); res=lp+rp; cout<<"The + result: "; res.Print(); res=lp-rp; cout<<"The - result: "; res.Print(); res=lp*rp; cout<<"The * result: "; res.Print(); res=cb; cout<<"The = result: "; res.Print(); return 0; }
最近写的关于C++ 复数运算符重载的,特分享一下
相关文章推荐
- C++项目——实现复数类中的运算符重载
- c++实现复数运算(运算符重载)
- 【C++】复数的运算符重载
- C++【面试题】:类实现万年历(日期计算器),(含构造函数、拷贝构造、运算符重载、析构函数)
- 第九周(项目一)——实现复数类中的运算符重载。
- c++复习一:复数运算的简单实现。
- 【c++】实现"String"类,完成运算符重载等
- 【c++】用c++实现复数类及运算符的重载
- 第八周项目一:实现复数类中的运算符重载(一)
- 第八周项目一——实现复数类中的运算符重载(1)
- 第8周项目1.1 类的成员函数实现复数类中的运算符重载
- 第十二周项目2:实现复数类中的运算符重载(2)
- 第12周项目——实现复数类中的运算符重载3
- C++复数运算符重载(+与<<)
- 第八周上机实践项目——实现复数类中的运算符重载(1)
- 八周 项目1 实现复数中的运算符重载
- 第八周(项目一)——实现复数类中的运算符重载(3)。
- C++【面试题】:类实现万年历(日期计算器),(含构造函数、拷贝构造、运算符重载、析构函数)
- 《C++第九周实验报告3-1》----接第8周任务3,定义分数类中<<和>>运算符重载,实现分数的输入输出
- 第十二周上机实践项目1(1):实现复数类中的运算符重载