其他分享
首页 > 其他分享> > 关系运算符重载(>,<,==,!=)

关系运算符重载(>,<,==,!=)

作者:互联网

关系运算符重载

#include<iostream>
using namespace std;
class wood {
public:
	int num;
	int height;
	//==运算符重载
	bool operator==(wood w)
	{
		if (num == w.num && height == w.height)
		{
			return true;
	 }
		return false;
	}
	//!=运算符重载
	bool operator!=(wood w)
	{
		if (num != w.num || height != w.height)
		{
			return true;
		}
		return false;
	}
};

void test()
{
	wood w1;
	w1.num = 100;
	w1.height=10;
	wood w2;
	w2.num = 100;
	w2.height = 11;
	if (w2 == w1)
	{
		cout << "w1和w2相等" << endl;
	}
	if(w2!=w1)
	{
		cout << "w1和w2不相等" << endl;
	}

}
int main()
{
	test();
	return 0;
}

标签:关系,wood,return,height,运算符,num,w2,重载
来源: https://blog.csdn.net/m0_53157173/article/details/113506399