第八周任务一
2012-04-11 22:08
302 查看
#include <iostream> using namespace std; class Complex { public: Complex(){real=0;imag=0;} Complex(double r,double i){real=r;imag=i;} Complex operator+(Complex &c2); Complex operator-(Complex &c2); Complex operator*(Complex &c2); Complex operator/(Complex &c2); void display(); private: double real; double imag; }; //下面定义成员函数 Complex Complex::operator+(Complex &c2) { return Complex (real + c2.real,imag + c2.imag); } Complex Complex::operator-(Complex &c2) { return Complex (real - c2.real,imag - c2.imag); } Complex Complex::operator*(Complex &c2) { Complex c; c.real = real * c2.real - imag * c2.imag; c.imag = real * c2.imag + imag * c2.real; return c; } Complex Complex::operator/(Complex &c2) { Complex c; c.real = (real * c2.real + imag * c2.imag)/(c2.imag * c2.imag + c2.real * c2.real); c.imag = (imag * c2.real - real * c2.imag)/(c2.imag * c2.imag + c2.real * c2.real); return c; } void Complex::display() { cout << "(" << real << "," << imag << "i)" << endl; } int main() { Complex c1(3,4),c2(5,-10),c3; cout<<"c1 = "; c1.display(); cout<<"c2 = "; c2.display(); c3=c1+c2; cout<<"c1+c2 = "; c3.display(); c3=c1-c2; cout<<"c1-c2 = "; c3.display(); c3=c1*c2; cout<<"c1*c2 = "; c3.display(); c3=c1/c2; cout<<"c1/c2 = "; c3.display(); system("pause"); return 0; }
相关文章推荐
- 第八周上机任务之求两点间的距离
- 第八周上机任务(加密)
- C++程序设计实验报告(五十一)---第八周任务1-1
- 第八周任务1方案2
- 第八周上级任务1
- 第八周任务二(实现Time类中的运算符重载)
- 第八周任务(二)
- 第八周上机任务
- 第八周上级任务
- 第八周实验报告任务1
- 第八周上级任务之关于圆的计算
- C#--第八周实验--任务1--设计一个类,该类继承于已经设计好的接口
- C++程序设计实验报告(五十二)---第八周任务1-2
- 第八周实验报告(任务一)【复数与复数的加减乘除】
- 第八周任务1-2
- 《第八周任务一-3》实现复数类中的运算符的重载函数
- 第八周上机任务(一)
- 第八周任务一
- 第八周项目一任务*1*成员函数
- 第八周任务二:实现Time 类中的运算符重载