C++:運算符重載

class complex
{
	public:
		double real,imag;
		complex(double r=0.0,double i=0.0):real(r),imag(i){ };
		complex operator-(const complex &c);	
};
complex operator+(const complex &a,const complex &b)
{
	return complex(a.real+b.real,a.imag+b.imag);//返回臨時對象
}//重載爲普通函數,參數個數 運算符目數 

complex::operator-(const complex &c)
{
	return complex(real-c.real+imag-c.imag);//返回臨時對象
	//重載爲成員函數,參數個數爲 運算符目數-1 
 } //real 爲當前對象所指
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章