运算符重载的三种方法

 

#include<iostream.h>
class Complex{
	public:
		Complex(int a,int b):x(a),y(b){}
		friend const Complex operator+(Complex &c1,Complex &c2);
	//	friend const Complex operator-(Complex &c1,Complex &c2);  //operator-不是友元函数
		const Complex operator*(Complex &c1); //是成员函数的时候
		friend const Complex operator/(Complex &c1,Complex &c2);

	public:
		void show();
		int getX();
		int getY();
	private:
		int x;
		int y;
};
inline const Complex operator+(Complex &c1,Complex &c2)
{
	return Complex(c1.x+c2.x,c1.y+c2.y);
}
inline const Complex operator-(Complex &c1,Complex &c2)
{
	return Complex(c1.getX()-c2.getX(),c1.getY()-c2.getY());
}
const Complex Complex::operator*(Complex &c1)
{
	return Complex(this->x*c1.getX(),this->y*c1.getY());
}
inline const Complex operator/(Complex &c1,Complex &c2)
{
	if(c2.x==0||c2.y==0)
		return Complex(0,0);
	return Complex(c1.x/c2.x,c1.y/c2.y);
}

void Complex::show()
{
	cout<<"x:"<<x<<endl;
	cout<<"y:"<<y<<endl;
}
int Complex::getX()
{
	return x;
}
int Complex::getY()
{
	return y;
}
int main()
{

	Complex c1(1,1);  
	Complex c2(1,1);
	Complex c3=c1+c2;//相当于调用函数 operator+(c1,c2)
	c3.show();

	Complex c4=c1-c2;
	c4.show();

	Complex c5=c1*c2;//相当于调用函数 x.operator*(c1)
	c5.show();
	return 0;
}
### 两种实现运算符重载方法 在C++中,运算符重载可以通过两种主要方式实现:**作为类的成员函数**和**作为友元函数**。这两种方法各有特点,适用于不同的场景。 #### 1. 成员函数实现 当运算符重载作为类的成员函数时,左操作数隐式地通过`this`指针传递[^4]。这意味着对于双目运算符,只需要显式声明一个参数来表示右操作数,而左操作数由调用该成员函数的对象提供。例如: ```cpp class Complex { private: double real; double imag; public: Complex(double r = 0, double i = 0) : real(r), imag(i) {} // 加号运算符重载 Complex operator+(const Complex& c) { return Complex(real + c.real, imag + c.imag); } }; ``` 在此示例中,`operator+`作为成员函数实现,左操作数是调用对象,右操作数是传入的参数[^2]。 #### 2. 友元函数实现 当运算符重载作为友元函数时,所有操作数都需要显式地作为参数传递给函数[^4]。这种方式允许更灵活的操作数类型定义,尤其是在需要将非类类型的对象作为左操作数时非常有用。例如: ```cpp class Complex { private: double real; double imag; public: Complex(double r = 0, double i = 0) : real(r), imag(i) {} // 声明友元函数 friend Complex operator+(const Complex& c1, const Complex& c2); }; // 定义友元函数 Complex operator+(const Complex& c1, const Complex& c2) { return Complex(c1.real + c2.real, c1.imag + c2.imag); } ``` 在这个例子中,`operator+`被定义为友元函数,两个操作数都需要显式地传递给函数[^3]。 #### 方法选择 - **成员函数**更适合于操作数主要是类对象本身的情况,因为它简化了参数列表,并且自然地与类的封装性结合。 - **友元函数**则适合于需要处理非类类型的左操作数或需要更灵活的操作数组合的情况[^1]。 ### 示例代码 以下是一个完整的代码示例,展示了两种方法的实现: ```cpp #include <iostream> using namespace std; class Complex { private: double real; double imag; public: Complex(double r = 0, double i = 0) : real(r), imag(i) {} // 成员函数实现加法 Complex operator+(const Complex& c) { return Complex(real + c.real, imag + c.imag); } // 声明友元函数实现流插入运算符 friend ostream& operator<<(ostream& os, const Complex& c); }; // 定义友元函数实现流插入运算符 ostream& operator<<(ostream& os, const Complex& c) { os << c.real << " + " << c.imag << "i"; return os; } int main() { Complex c1(3, 4); Complex c2(1, 2); Complex c3 = c1 + c2; // 使用成员函数实现 cout << "c1 + c2 = " << c3 << endl; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值