#include<iostream.h>
 class complex	//复数类的定义
{
 public:	//外部接口
  complex(double r=0.0,double i=0.0);//构造函数
  complex(complex &p);//拷贝构造函数     
  ~complex();//析构函数
  complex& operator +(complex &c);//复数加法
  complex& operator -(complex &c);//复数减法
  complex& operator *(complex &c);//复数乘法
  complex& operator /(complex &c);//复数除法
  friend ostream& operator <<(ostream& out,complex &s);//显示复数的值
 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;
}
 complex& complex::operator +(complex &c)
{
 return complex(real+c.real,imag+c.imag);  
}
 complex& complex::operator -(complex &c)
{
 return complex (real-c.real,imag-c.imag);
}
 complex& complex::operator *(complex &c)
{
 complex temp;
 temp.real=real*c.real-imag*c.imag;
 temp.imag=imag*c.real+real*c.imag; 
 return (temp);
}
 complex& complex::operator /(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);
}
 ostream& operator <<(ostream& out,complex &s)
{
 out<<s.real;
 if(s.imag>=0)
  cout<<"+";
 out<<s.imag<<"i ";
 return(out);
}
 //复数类的应用
 int main()	//主函数
{
 complex a(1,3),b(2,4),c;//定义复数类的对象
 cout<<"a="<<a<<endl;
 cout<<"b="<<b<<endl;
 c=a+b;
 cout<<"c="<<c<<endl;
 c=a-b;  
 cout<<"c="<<c<<endl;
 c=a*b;
 cout<<"c="<<c<<endl;
 c=a/b;
 cout<<"c="<<c<<endl;
 cout<<a<<b<<c<<endl;
 return(0);
}

