码迷,mamicode.com
首页 > 编程语言 > 详细

C++ class外的==重载,判断相等,测试等于,重载示例。二元操作符

时间:2019-12-01 11:53:05      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:clu   end   ++   std   load   idt   ide   height   iostream   

#include <iostream>

// overloading "operator == " outside class
// == 是二元操作符

//////////////////////////////////////////////////////////

class Rectangle
{
public:
	Rectangle(int w, int h) 
		: width(w), height(h)
	{};

	~Rectangle() {};

	//bool operator== (Rectangle& rec);


public:
	int width;
	int height;
};

//////////////////////////////////////////////////////////

bool operator==(Rectangle & ths, Rectangle & rec)
{
	return ths.height == rec.height
		&& ths.width == rec.width;
}

//////////////////////////////////////////////////////////

int main()
{
	Rectangle a(40, 10);
	Rectangle b(40, 10);
	Rectangle c(4, 10);

	std::cout << (a == b) << std::endl;
	std::cout << (a == c) << std::endl;
	std::cout << (b == c) << std::endl;

	return 0;
}

  

C++ class外的==重载,判断相等,测试等于,重载示例。二元操作符

标签:clu   end   ++   std   load   idt   ide   height   iostream   

原文地址:https://www.cnblogs.com/alexYuin/p/11965139.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!