C++中运算符的重载

2024-11-27 04:34:14
推荐回答(1个)
回答1:

#include
using namespace std;
class Complex
{
public:
Complex (int xx=0,int yy=0);
Complex operator + (Complex t);
Complex & operator = (Complex t);
void show();
private:
int x,y;
};

Complex::Complex (int xx,int yy)
{
x=xx;
y=yy;
}

Complex Complex ::operator + (Complex t)
{
Complex temp;
temp.x=x+t.x;
temp.y=y+t.y;
return temp;
}

Complex & Complex:: operator = (Complex t)
{
x=t.x;
y=t.y;
return *this;
}

void Complex::show ()
{
cout<}

void main ()
{
Complex a(1,2), b(2,3),c;
c=a+b;
c.show();

}

好了在vc2008下编译成功