C++04运算符重载(复数的运算),友元函数

1.运算符重载

C++ 运算符的重载
目的:让对象的运算表现的和内置类型一样

模板
template
bool comapre(T a, T b)
{
cout<<a<<endl;
return a > b;
}
对象的运算,都被转化成左边对象,调用运算符方法,
// 把在右边的对象当作实参传入进来
// comp1.operator+(comp2)
// CComplex operator+(const CComplex &src)
编译器优先去类里面查找运算符重载函数,如果没有,然后
// 再到全局找合适的运算符重载函数

1.实例(复数的加减运算)

class CComplex
{
public:
	// CComplex()  CComplex(int)  CComplex(int, int)
	CComplex(int r = 10, int i = 10)
		:mreal(r), mimage(i) 
	{}
	~CComplex(){}
	CComplex(const CComplex &src)
		:mreal(src.mreal), mimage(src.mimage)
	{}
	CComplex& operator=(const CComplex &src)
	{
		mreal = src.mreal;
		mimage = src.mimage;
		return *this;
	}
	//comp3 = comp1 + comp2;  comp1.operator+(comp2)
	/*CComplex operator+(const CComplex &src)
	{
		CComplex temp;
		temp.mreal = mreal + src.mreal;
		temp.mimage = mimage + src.mimage;
		return temp;
		return CComplex(mreal + src.mreal,
			mimage + src.mimage);
	}*/
	void operator+=(const CComplex &src)
	{
		mreal += src.mreal;
		mimage += src.mimage;
	}
	CComplex& operator++()  // comp1 = ++comp3;
	{
		mreal++;
		mimage++;
		return *this;
	}
	CComplex operator++(int)
	{
		//CComplex temp = *this;
		//mreal++;
		//mimage++;
		//return temp;
		return CComplex(mreal++, mimage++);
	}
private:
	int mreal;
	int mimage;

	//提供友元方法 => 声明  友元关系是单向的,不可传递的
	friend ostream& operator<<(ostream &out, const CComplex &src);
	friend istream& operator>>(istream &in, CComplex &src);
	friend CComplex operator+(const CComplex &lhs, const CComplex &rhs);
};
ostream& operator<<(ostream &out, const CComplex &src)
{
	out << "real:" << src.mreal << " image:" << src.mimage;
	return out;
}
//cin >> comp3 >> comp1;
istream& operator>>(istream &in, CComplex &src)
{
	in >> src.mreal >> src.mimage;
	return in;
}
CComplex operator+(const CComplex &lhs, const CComplex &rhs)
{
	return CComplex(lhs.mreal + rhs.mreal,
		lhs.mimage + rhs.mimage);
}
int main()
{
	CComplex comp1(10, 10);
	CComplex comp2 = comp1;
	CComplex comp3 = CComplex(20, 20);
	comp3 = comp1;
	comp3 = 20;  // CComplex(20)
	comp3 = (CComplex)20;
	// 对象的运算,都被转化成左边对象,调用运算符方法,
	// 把在右边的对象当作实参传入进来
	//      comp1.operator+(comp2)
	//  CComplex operator+(const CComplex &src)    
	comp3 = comp1 + comp2;
	// 编译器优先去类里面查找运算符重载函数,如果没有,然后
	// 再到全局找合适的运算符重载函数
	// ::operator<<(cout, comp3)
	// void operator<<(ostream &out, const CComplex &src)
	// ::operator<<(cout, comp3) cout << endl;
	cout << comp3 << endl;  // ostream   comp3 << cout
	cin >> comp3;
	cout << comp3 << endl;
	// comp3.operator+=(comp1)  void operator+=(const CComplex &src)
	comp3 += comp1; 
	cout << comp3 << endl;

	comp1 = comp3++;   // comp3.operator++(int)
	cout << comp1 << endl;
	cout << comp3 << endl;
	comp1 = ++comp3;   // comp3.operator++()
	cout << comp1 << endl;
	cout << comp3 << endl;

	comp1 = comp3 + 10;  // comp3.operator+(10) const CComplex &src
	comp1 = 10 + comp3;
	cout << comp1 << endl;
	// 
}

2.友元函数

目的:使其他类或在类外可以使用成员方法
友元函数是可以直接访问类的私有成员的非成员函数。它是定义在类外的普通函数,它不属于任何类,但需要在类的定义中加以声明,声明时只需在友元的名称前加上关键字friend。

//提供友元方法 => 声明 友元关系是单向的,不可传递的
friend ostream& operator<<(ostream &out, const CComplex &src);
friend istream& operator>>(istream &in, CComplex &src);
friend CComplex operator+(const CComplex &lhs, const CComplex &rhs);

猜你喜欢

转载自blog.csdn.net/sunshinecandy/article/details/89279427
今日推荐