回复 8楼 pangding
实在不好意思,刚才太急,有很多错误
class Complex
{
int real,image;
public:
Complex(real=0,image=0):real(real),image(image){}
Complex operator +(complex& b)
{
Complex t;
t.real=real+b.real;
t.image=image+b.image;
return t;
}
Complex& operator =(Complex& c)
{
real=c.real;
image=c.image;
}
void show(int i)
{
cout<<"c"<<i<<"="<<real<<"+"<<image<<"i"<<endl;
}
};
int main()
{
Complex c1(10,20),c2(12,24),c3;
c3=c1+c2;
c3.show(3);
}
class F//n为分子,f为分母
{
int n;
int d;
public:
F(int n=0,int d=1):n(n),d(d){}
F operator+(F& c)
{
F t;
t.n=n*c.d+d*c.n;
t.d=d*c.d;
return t;
}
friend ostream& operator<<(ostream& o,const F& f)
{
o<<f.n<<"/"<<f.d
return o;
}
F& operator =(F& c)
{
n=c.n;
d=c.d;
}
};
int main()
{
F c1(1,2),c2(2,3),c3;
c3=c1+c2;
cout<<"c3="<<c3<<endl;
}
为什么第一个会错,第二个没错,当第一个在=重载的时候的形参加const就对了(const Complex& c)
[
本帖最后由 chinawide 于 2013-1-22 13:03 编辑 ]