class Complex //Definitiong of class complex
{
public:
Complex(double r=0,double i=0){ mRel = r; mImag = i;}//constructor
Complex operator +(Complex c); //Reuse operator '+'
Complex operator -(Complex c); //Reuse operator '-'
void Display(); //Output complex
private:
double mRel,mImag;
};
Complex Complex::operator +(Complex c)
{
//Complex cc;
//cc.mRel = mRel + c.mRel;
//cc.mImag = mImag + c.mImag;
double a = mRel + c.mRel;
double b = mImag + c.mImag;
return Complex(a,b);
}
Complex Complex::operator -(Complex c)
{
Complex cc;
cc.mRel = mRel - c.mRel; //why can access private member declared in class 'Complex'*********
cc.mImag = mImag - c.mImag;
return Complex(cc.mRel,cc.mImag);
}
void Complex::Display()
{
cout<<"("<<mRel<<" "<<mImag<<")"<<endl;
}
void main()
{
Complex c1(5,4),c2(2,10),c3;
cout<<"c1="; c1.Display();
cout<<"c2="; c2.Display();
c3 = c1 - c2;
cout<<"c3=c1-c2=";
c3.Display();
c3 = c1 + c2;
cout<<"c3=c1+c2=";
c3.Display();
//cout<<c3.mImag<<endl; //***********
}
大家看有**********的两个注释行,问题就在注释行里!!!