我定义了个复数类的运算符重载,为什么不能访问私有数据
我定义了个复数类的运算符重载,为什么不能访问私有数据,但是书上这样可以啊,另外我定义的一个分数类运算符重载,它可以访问私有数据.........
复数类:
#include <iostream.h>
#include <stdio.h>
#include <math.h>
class Complex
{
private:
double real;
double imag;
public:
//double real;
//double imag;
Complex()
{
}
Complex(double x,double y)
{
real=x;
imag=y;
}
Complex(char *d)
{
char look;
sscanf(d,"%lf%c",&real,&look);
if(look=='-'||look=='+')
{
sscanf(d,"%lf%c%lf%*c",&real,&look,&imag);
if(look=='-')
imag=-imag;
}
else imag=0;
}
friend ostream& operator<<(ostream&,const Complex &temp);
friend istream& operator>>(ostream&,const Complex &temp);
Complex operator+(const Complex &com);
Complex operator-(const Complex &com);
Complex operator*(const Complex &com);
Complex operator/(const Complex &com);
Complex operator+(double d);
Complex operator-(double d);
Complex operator*(double d);
Complex operator/(double d);
friend Complex operator+(double d,const Complex &com);
friend Complex operator-(double d,const Complex &com);
friend Complex operator*(double d,const Complex &com);
friend Complex operator/(double d,const Complex &com);
int operator==(const Complex &);
int operator!=(const Complex &);
void Display() const
{
cout<<endl<<real;
if(imag>0)
{
cout<<"+";
cout<<imag<<"i"<<endl;
}
else if(imag==0);
else cout<<imag<<"i"<<endl;
}
};
ostream& operator<<(ostream& out,const Complex &temp)
{
temp.Display();
return out;
}
istream& operator>>(istream& in, Complex &temp)
{
char d[10];
char look;
cin>>d;
sscanf(d,"%lf%c",&temp.real,&look);
if(look=='-'||look=='+')
{
sscanf(d,"%lf%c%lf%*c",&temp.real,&look,&temp.imag);
if(look=='-')
temp.imag=-temp.imag;
}
else temp.imag=0;
return in;
}
Complex Complex::operator+(const Complex &com)
{
Complex temp;
temp.real=real+com.real;
temp.imag=imag+com.imag;
return temp;
}
Complex operator+(double d,const Complex &com)
{
Complex temp;
temp.real=com.real+d;
temp.imag=com.imag;
return temp;
}
Complex Complex::operator+(double d)
{
Complex temp;
temp.real=real+d;
temp.imag=imag;
return temp;
}
Complex Complex::operator-(const Complex &com)
{
Complex temp;
temp.real=real-com.real;
temp.imag=imag-com.imag;
return temp;
}
Complex operator-(double d,const Complex &com)
{
Complex temp;
temp.real=d-com.real;
temp.imag=com.imag;
return temp;
}
Complex Complex::operator-(double d)
{
Complex temp;
temp.real=real-d;
temp.imag=imag;
return temp;
}
Complex Complex::operator*(const Complex &com)
{
Complex temp;
temp.real=(com.real)*real+(com.imag)*imag;
temp.imag=(com.imag)*real+(com.real)*imag;
return temp;
}
Complex operator*(double d,const Complex &com)
{
Complex temp;
temp.real=d*com.real;
temp.imag=com.imag*d;
return temp;
}
Complex Complex::operator*(double d)
{
Complex temp;
temp.real=real*d;
temp.imag=imag*d;
return temp;
}
Complex Complex::operator/(const Complex &com1)//////////////////////
{
if( (com1.imag==0)&&(com1.real==0) )
{
cout<<"dived con't be zero!"<<endl;
return *this;
}
Complex temp,com;
double tempp;
com=com1;
temp=*this;
tempp=com.real*com.real+com.imag*com.imag;
com.imag=-com.imag;
temp=temp*com;
temp=temp/tempp;
return temp;
}
Complex Complex::operator/(double d)
{
if(d==0)
{
cout<<"dived con't be zero!"<<endl;
return *this;
}
Complex temp;
temp.real=real/d;
temp.imag=imag/d;
return temp;
}
Complex operator/(double d,const Complex &com)/////////////////////////////
{
if( (com.imag==0)&&(com.real==0) )
{
cout<<"dived con't be zero!"<<endl;
return 0;
}
Complex temp,com1;
double tempp;
com1=com;
com1.imag=-com1.imag;
tempp=com.real*com.real+com.imag*com.imag;
d=d/tempp;
temp=d*com1;
return temp;
}
int Complex::operator==(const Complex &temp)
{
if(real!=temp.real)
return 0;
if(imag!=temp.imag)
return 0;
return 1;
}
int Complex::operator!=(const Complex &temp)
{
if(real!=temp.real)
return 1;
if(imag!=temp.imag)
return 1;
return 0;
}