#include<iostream.h>
 class complex	//复数类的定义
{
 public:	//外部接口
  complex(double r=0.0,double i=0.0);//构造函数
  complex(complex &p);//拷贝构造函数     
  ~complex();//析构函数
  void display(void);//显示复数的值
  complex& add(complex &c);//复数加法
  complex& sub(complex &c);//复数减法
  complex& mul(complex &c);//复数乘法
  complex& div(complex &c);//复数除法
 private:	//私有数据成员
  double real;//复数实部
  double imag;//复数虚部
};
 //复数类的实现
 complex::complex(double r,double i)
{
 real=r;
 imag=i;
 cout<<"复数对象创建"<<endl;
}
 complex::complex(complex &p)
{
 real=p.real;
 imag=p.imag;
 cout<<"复数对象创建"<<endl;
}
 complex::~complex()
{
 cout<<"复数对象结束"<<endl;
}
 void complex::display(void)
{
 cout<<real;
 if(imag>=0)
  cout<<"+";
 cout<<imag<<"i"<<endl;
}
 complex& complex::add(complex &c)
{
 return complex(real+c.real,imag+c.imag);  
}
 complex& complex::sub(complex &c)
{
 return complex (real-c.real,imag-c.imag);
}
 complex& complex::mul(complex &c)
{
 complex temp;
 temp.real=real*c.real-imag*c.imag;
 temp.imag=imag*c.real+real*c.imag; 
 return (temp);
}
 complex& complex::div(complex &c)
{
 complex temp;
 double t;
 t=c.real*c.real+c.imag*c.imag;
 temp.real=(real*c.real+imag*c.imag)/t;
 temp.imag=(imag*c.real-real*c.imag)/t; 
 return (temp);
}
 //复数类的应用
 int main()	//主函数
{
 complex f1(3,4),f2(1,2),f3(f1);//定义复数类的对象
 cout<<"f1=";
 f1.display();
 cout<<"f2=";
 f2.display();
 cout<<"f3=";
 f3.display();
 f3=f1.add(f2);//f1+f2  
 cout<<"f3=";
 f3.display();
 f3=f1.sub(f2);//f1-f2  
 cout<<"f3=";
 f3.display();
 f3=f1.mul(f2);//f1*f2  
 cout<<"f3=";
 f3.display();
 f3=f1.div(f2);//f1/f2  
 cout<<"f3=";
 f3.display();
 return(0);
}
