C++ class内的 < 和 > 重载,大于号,小于号,重载示例。
2024-10-12 21:10:52
#include <iostream> // overloading "operator = " outside class
// < 和 > 是二元操作符 ////////////////////////////////////////////////////////// class Rectangle
{
public:
Rectangle(int w, int h)
: width(w), height(h)
{}; ~Rectangle() {}; bool operator< (Rectangle& rec);//注意比较的顺序。this是被比的对象。
bool operator> (Rectangle& rec); public:
int width;
int height;
}; bool
Rectangle::operator< (Rectangle & rec)//相同的class对象互为友元,所以可以访问private对象。< 是二元操作符,class内隐藏了this
{
return this->height * this->width < rec.height * rec.width;
} bool
Rectangle::operator> (Rectangle & rec)//二元操作符,class内隐藏了this
{
return !(*this < rec);
} ////////////////////////////////////////////////////////// int main()
{
Rectangle a(40, 10);
Rectangle b(40, 56); std::cout << (a < b) << std::endl;
std::cout << (a > b) << std::endl; return 0;
}
最新文章
- 用PHP抓取页面并分析
- SQLMAP 中$与#的区别
- SparkSql 不支持Date Format (支持Timestamp)
- LA 3890 (半平面交) Most Distant Point from the Sea
- less-2
- 同时处理html+js+jquery+css的插件安装(Spket&;Aptana插件安装)
- (简单) POJ 1961 Period,扩展KMP。
- QT的安装及环境配置
- An internal error occurred during: ";Requesting JavaScript AST from selection";. GC overhead limit exc
- ZOJ-1655 Transport Goods---dijkstra变形&;&;最长路
- MongoDB 数据库引用
- 两种方法轻松搞定-- Eclipse 安装FindBugs插件
- jenkins忘记admin密码解决办法
- 在Django中接收文件并存储
- 使用ASP.NET Core的User Secrets特性
- it做形式主语的句子
- PHP-数据库永久连接
- UVALive.3708 Graveyard (思维题)
- pyCharm上解决安装不上pandas库问题
- MFC学习(三)程序入口和执行流程