//02.* Copyright (c) 2013, 烟台大学计算机学院
//04.* 作 者: 马德鹏
//05.* 完成日期:2014 年 4 月18 日
//06.* 版 本 号:v1.0
//07.*
#include <iostream>
#include <cmath>
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)
{
Complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;
}
Complex Complex::operator-(Complex &c2)
{
Complex c;
c.real=real-c2.real;
c.imag=imag-c2.imag;
return c;
}
Complex Complex::operator*(Complex &c2)
{
Complex c;
c.real=real*c2.real;
c.imag=imag*c2.imag;
return c;
}
Complex Complex::operator/(Complex &c2)
{
Complex c;
c.real=real/c2.real;
c.imag=imag/c2.imag;
return c;
}
void Complex::display()
{
cout<<"("<<real<<","<<imag<<")"<<endl;
}
//下面定义用于测试的main()函数
int main()
{
Complex c1(3,4),c3(5,-10),c4;
cout<<"c1=";
c1.display();
cout<<"c3=";
c4.display();
c4=c1+c3;
cout<<"c1+c3=";
c4.display();
c4=c1-c3;
cout<<"c1-c3=";
c3.display();
c3=c1*c3;
cout<<"c1*c2=";
c4.display();
c4=c1/c3;
cout<<"c1/c3=";
c4.display();
return 0;
}
费了老牛鼻子劲了,终于把这篇博文发了,宿舍的网伤不起哈,