为什么构造函数在外面不行?高手帮忙啊!
class Complex{
private:
double real;
double imag;
public:
Complex (double r,double i);
Complex operator+(Complex &s);
Complex operator-(Complex &s);
Complex operator*(Complex &s);
Complex operator/(Complex &s);
void show();
};
Complex::Complex(double r=0,double i=0)
{
real=r;
imag=i;
}
Complex Complex::operator+(Complex &s)
{
double r=real+s.real;
double i=imag+s.imag;
return Complex(r,i);
}
Complex Complex::operator-(Complex &s)
{
double r=real-s.real;
double i=imag-s.imag;
return Complex(r,i);
}
Complex Complex::operator*(Complex &s)
{
double r=real*s.real-imag*s.imag;
double i=real*s.imag+imag*s.real;
return Complex(r,i);
}
Complex Complex::operator/(Complex &s)
{
double d=s.real*s.real+s.imag*s.imag;
if(d==0) {cout<<"分母为0,错!"<<endl;exit(1);}
else
{double r=(real*s.real+imag*s.imag)/d;
double i=(imag*s.real-real*s.imag)/d;
return Complex(r,i);
}
}
void Complex::show()
{
if(real!=0) cout<<real;
else ;
if(imag<0) cout<<"-"<<imag<<"i"<<endl;
if(imag==0) cout<<endl;
if(imag>0) cout<<"+"<<imag<<"i"<<endl;
}
void main()
{
Complex c1(1,2),c2(3,4),c3,c4,c5,c6;
c3=c1+c2;
c4=c1-c2;
c5=c1*c2;
c6=c1/c2;
c1.show();
c2.show();
c3.show();
c4.show();
c5.show();
c6.show();
}