#include <iostream>

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


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

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

	~Rectangle() {};

	bool operator == (Rectangle& rec) const;


private:
	int width;
	int height;
};

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

bool Rectangle::operator==(Rectangle & rec) const//相同的class对象互为友元,所以可以访问private对象。== 是二元操作符,class内隐藏了this
{
	return this->height == rec.height
		&& this->width == rec.width;
}

// 等价于:
/*
bool Rectangle::operator==(Rectangle* this, Rectangle & rec) const
{
	return this->height == rec.height
		&& this->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;
}

  

相关文章:

  • 2022-03-03
  • 2021-11-07
  • 2021-09-11
  • 2022-02-08
  • 2021-08-24
  • 2021-10-10
  • 2021-12-09
猜你喜欢
  • 2021-07-04
  • 2022-12-23
  • 2022-02-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-02
相关资源
相似解决方案