C++系列-关系运算符重载

文章介绍了如何在C++中重载关系运算符,如==和!=,以实现对自定义类型Horse的比较,并展示了拷贝构造函数的使用。测试部分显示了两个Horse对象的相等性和不等性判断。

关系运算符重载


关系运算符重载可以实现自定义类型的比较

code:
	#include <iostream>
	using namespace std;
	class Horse
	{
	private:
		int m_age;
		string m_color;
	public:
		Horse(int ref_age, string ref_color)		// 有参构造
		{
			m_age = ref_age;
			m_color = ref_color;
		}
		Horse(const Horse& ref_h)					// 拷贝构造
		{
			m_age = ref_h.m_age;
			m_color = ref_h.m_color;
		}
	
		bool operator==(const Horse& ref_h)		// 返回自身的引用,链式编程,实现连等操作
		{
			if (m_age == ref_h.m_age && m_color == ref_h.m_color)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		bool operator!=(const Horse& ref_h)
		{
			if (m_age == ref_h.m_age && m_color == ref_h.m_color)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
	
		void show_info()
		{
			cout << "age: " << m_age << ", color: " << m_color << endl;
		}
	};
	
	// 对拷贝构造函数的测试
	void test01()
	{
		Horse h1(0, "white");
		Horse h2(h1);
		cout << (h1 == h2) << endl;
		cout << (h1 != h2) << endl;
	}
	
	void main()
	{
		test01();
		system("pause");
	}
result:
	1
	0
C++中的运算符重载是一种允许用户为已有的运算符赋予新的含义的机制,通常用于自定义类的对象操作,以增强代码的直观性和可读性。运算符重载的本质是函数重载,其核心思想是通过定义特定的函数来改变运算符对用户定义类型的行为。 ### 重载运算符的语法格式 运算符重载函数的定义形式如下: ```cpp 返回类型 operator 运算符(参数列表) { // 函数体 } ``` 例如,如果希望为一个自定义类`Complex`(复数类)重载加法运算符`+`,可以定义如下函数: ```cpp Complex operator+(const Complex& c1, const Complex& c2); ``` ### 重载运算符的方式 运算符重载可以通过两种主要方式实现:**成员函数重载**和**友元函数重载**。 #### 成员函数重载 当运算符被重载为类的成员函数时,第一个操作数隐式地成为调用该函数的对象。因此,成员函数重载仅需要显式声明一个参数(除非是单目运算符)。例如,重载`+`运算符作为`Complex`类的成员函数: ```cpp class Complex { public: Complex(double real, double imag) : real(real), imag(imag) {} Complex operator+(const Complex& other) const { return Complex(real + other.real, imag + other.imag); } private: double real; double imag; }; ``` #### 友元函数重载 当需要对两个操作数进行对称处理时,可以将运算符重载为类的友元函数。这种方式允许两个操作数都被显式传递,通常用于二元运算符。例如,重载`+`运算符作为`Complex`类的友元函数: ```cpp class Complex { public: Complex(double real, double imag) : real(real), imag(imag) {} friend Complex operator+(const Complex& c1, const Complex& c2); private: double real; double imag; }; Complex operator+(const Complex& c1, const Complex& c2) { return Complex(c1.real + c2.real, c1.imag + c2.imag); } ``` ### 运算符重载的限制 尽管运算符重载提供了强大的功能,但也存在一些限制: - 不能重载C++中已有的运算符以外的符号,例如不能创建新的运算符。 - 有些运算符不能被重载,例如作用域解析运算符`::`、成员访问运算符`.`、条件运算符`?:`等。 - 重载运算符的优先级和结合性不能更改。 - 重载运算符必须至少有一个操作数是用户定义类型。 ### 示例:重载`<<`用于输出 通常,为了方便输出自定义类的对象,会重载流输出运算符`<<`。由于`<<`的左操作数是标准库中的`ostream`对象,因此必须使用友元函数重载: ```cpp class Complex { public: Complex(double real, double imag) : real(real), imag(imag) {} friend std::ostream& operator<<(std::ostream& os, const Complex& c); private: double real; double imag; }; std::ostream& operator<<(std::ostream& os, const Complex& c) { os << c.real << " + " << c.imag << "i"; return os; } ``` 通过上述方式,可以实现C++中运算符重载的基本方法,从而增强代码的可读性和易用性[^1]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值