展开全部
所谓全局函数,就是用友元来实现。 多说无益 看代码吧:
class COMPLEX
{
private:
double realPart,imagePart;
public:
friend COMPLEX operator + (COMPLEX &a,COMPLEX &b);
};
COMPLEX operator + (COMPLEX &a,COMPLEX &b)
{
COMPLEX temp;
temp.realPart=a.realPart+b.realPart;
temp.imagePart=a.imagePart+b.imagePart;
return temp;
}
/////////////////////////////////
class COMPLEX1
{
private:
double realPart,imagePart;
public:
COMPLEX1 operator + (COMPLEX1 &a);
};
COMPLEX1 COMPLEX1::operator + (COMPLEX1 &a)
{
COMPLEX1 temp;
temp.realPart=a.realPart+realPart;
temp.imagePart=a.imagePart+imagePart;
return temp;
}
class COMPLEX
{
private:
double realPart,imagePart;
public:
friend COMPLEX operator + (COMPLEX &a,COMPLEX &b);
};
COMPLEX operator + (COMPLEX &a,COMPLEX &b)
{
COMPLEX temp;
temp.realPart=a.realPart+b.realPart;
temp.imagePart=a.imagePart+b.imagePart;
return temp;
}
/////////////////////////////////
class COMPLEX1
{
private:
double realPart,imagePart;
public:
COMPLEX1 operator + (COMPLEX1 &a);
};
COMPLEX1 COMPLEX1::operator + (COMPLEX1 &a)
{
COMPLEX1 temp;
temp.realPart=a.realPart+realPart;
temp.imagePart=a.imagePart+imagePart;
return temp;
}
来自:求助得到的回答
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询