cpp_go_hdc2

//operator overloading(操作符重载-1,成员函数) this

/*
//标准库内复数的设计
inline complex&
__doapl(complex* ths, const complex& r)     //doapl = do assignment plus
{
	ths->re += r.re;
	ths->im += r.im;
	return *ths;

}
inline complex&
complex::operator += (const complex& r)
{
	return __doapl (this, r);
}


-----------------------------------------
{
	complex c1(2,1);
	complex c2(5);

	c2 += c1;
}
//对+=操作符的设计 >>>>>>>>>>>>>>>>>>>>>
inline complex&
complex::operator += (this,const complex& r)		//c2->this  c1->r
{
	return __doapl(this, r);
}
*/

//return by reference语法分析
// 传递者无需知道接收者是以reference形式接收
/*
inline complex&
__doapl(complex* ths, const complex& r)
{

	...
	return *ths;
}

inline complex&
complex::operator += (const complex& r)			//此处返回类型不可以是void eg: c3 += c2 += c1;
{
	return __doapl(this,r);
}
*/

//class body  之外的各种定义(definitions)
/*
inline double
imag(const complex& x)   //全域函数(global)
{
	return x.imag ();
}

inline double
real(const complex& x)
{
	return x.real ();
}
----------------------
{
	complex c1(2,1);

	cout << imag(c1);   //取得虚部
	cout << real(c1);	//取得实部
}
*/


//operator overloading(操作符重载-2,非成员函数) (无this)
//为了对付client的三种可能用法,对应开发三个函数

/*
//complex c1(2,1);
//complex c2;
inline complex   //c2= c1+ c2
operator + (const complex& x, const complex& y)
{
	return complex (real(x) +real(y),
					imag(x) +imag(y));
}
--------------------------------------------------
inline complex	//c2= c1 + 5
operator + (const complex& x,double y)
{
	return complex (real (x) + y, imag(x));
}
--------------------------------------------------
inline complex	//c2= 7 + c1
operator + (double x, const complex& y)
{
	return complex(x+real (y),imag(y));
}
*/

//temp object(临时对象) typename();
//以下这些函数绝不可return by reference,因为他们返回的必定是个local object
/*
inline complex
operator + (const complex& x, const complex& y)
{
	return complex (real (x) + real(y),
					imag (x) + imag(y));
}
--------------------------------------------------
inline complex
operator + (const complex& x, double y)
{
	return complex (real(x) +y, imag(x));
}
--------------------------------------------------
inline complex
operator + (double x,const complex& y)
{
	return complex (x +real(y),imag(y));
}
--------------------------------------------------
{
	int(7);
	complex c1(2,1);
	complex c2;
	complex();
	complex c3(4,5);

	cout<< complex (2);
}
*/


//class body 之外的各种定义(definitions)

/*
inline complex
operator + (const complex& x)
{
	return x;
}

inline complex
operator - (const complex& x)			//negate反向(取反)
{
	return complex (-real (x),-imag (x));		//函数内创建新的空间放新的结果(return by value)
}
-----------------------------------------------
{
	complex c1(2,1);
	complex c2;
	cout << -c1;
	cout << +c1;
}
*/

//operator overloading(操作符重载),非成员函数
/* 比较两复数相不相等
inline bool
operator == (const complex& x,
			 const complex& y)
{
	return real(x) == real(y)
		&& imag(x) == imag(y);
}
------------------------------------------------
inline bool
operator == (const complex& x, double y)
{
	return real(x) == y && imag (x) == 0;
}
------------------------------------------------
inline bool
operator == (double x, const complex& y)
{
	return x == real(y) && imag(y) == 0;
}
------------------------------------------------
{
complex c1(2,1);
complex c2;

cout << (c1==c2);
cout << (c1== 2);
cout << (0 == c2);
}
*/

/* 共轭复数
inline complex 
conj (const complex& x)
{	
	return complex (real (x),-imag(x));
}

#include 
ostream&
operator << (ostream& os, const complex& x)		//重载运算符<<
{	
	return os << '(' << real(x) << ','
			  << imag(x) << ')';
}
====================================================
{
	complex c1(2,1);						
	cout << conj(c1);			======>>>>> (2,-1)
	cout << c1 << conj(c1);					(2,1) (2,-1)
}
*/

你可能感兴趣的:(cpp_go_hdc2)