看看这个程序
#include<iostream>
using namespace std;
class Complex{
double real; //实数部分
double imag; //虚数部分
public:
Complex(double real=0,double imag=0){}
Complex operator+(Complex &s);
Complex operator+(int &s);
friend Complex operator+(int &s,Complex &s);
void disp();
};
Complex Complex::operator+(Complex &s)
/*{
Complex a;
a.real=real+s.real;
a.imag=imag+s.imag;
return a;
}*/
{
return Complex(real+s.real,imag+s.imag);
}
Complex Complex::operator+(int &s)
{
return Complex(real+s,imag);
}
Complex Complex::operator+(int &s , Complex &s1 )
{
return Complex(s+s1.real,s1.imag);
}
void Complex::disp()
{
cout<<"( "<<real<<","<<imag<<"i)";
}
int main()
{
Complex c1(3,3),c2(-1,0),c3,c4;
c3=c1+c2;
cout<<"c3=";
c3.disp();
int i=7;
c4=c1+i;
c3=i+c2;
c4.disp();
cout<<endl;
c3.disp();
return 0;
}
这个程序确定错误不少,多看看书吧,我也才学C++两周
天行健,君子以自强不息!!QQ:68660681