C++之类的比較运算符的重载
2024-08-30 15:56:54
比較运算符的重载通常有两种方式:
第一:作为成员函数重载
曾经几章的Student类为例:
<span style="font-family:Microsoft YaHei;font-size:18px;">class Student{
private:
string name;
int age;
float score;
//const成员变量
const int max_length;
//定义静态成员变量
static int number;
static float total;
public:
//Student(string name,int age,float score);
//有const成员变量,必须有參数初始化列表,
Student(string name,int age,float score):name(name),age(age),score(score),max_length(3){
number++;
total += score;
}
//拷贝构造函数中,const成员变量的初始化,用初始化列表
Student(const Student & s):max_length(3){
this ->name = s.name;
this ->age = s.age;
this ->score = s.score;
number++;
total += score;
};
~Student();
void setName(string n);
string getName()const;
void setAge(int a);
int getAge() const;
void setScore(float s);
float getScore() const;
void say() const;
static float getAverage(); //运算符的重载
bool operator== (const Student &s) const;
//用友元函数重载等于 运算符
//friend bool operator== (const Student &s,const Student&s1);
};
</span>
这里能够把(opetator==)理解为"成员函数名"。
bool Student::operator==(const Student &s) const {
return this->name == s.name && this->age == s.age && this ->score == s.score;
}
第二:作为友元函数重载
bool operator== (const Student &s,const Student&s1){
return (s.age == s1.age && s.name == s1.name && s.score == s1.score);
}
最新文章
- 【BZOJ3036】绿豆蛙的归宿 概率与期望
- IOS的APNS和PushKit门道详述
- Linux下新的网络管理工具ip替代ifconfig零压力
- [Js]JavaScript闭包和范围的快速测试
- Asp.net中从后台中如何获取html控件
- 1.1Android系统架构
- CodeForces 321A
- Free Pascal的IDE界面乱码解决方法
- 安卓工程修改包名后 Failed to find provider info for...问题
- Replace不区分大小写
- zoj 2071 Technology Trader 最大权闭合子图
- Entity Framework 学习中级篇3—存储过程(中)
- PAT (Advanced Level) 1019. General Palindromic Number (20)
- shell 快速入门
- linux下载时提示请尝试移除磁盘中不需要的文件并重试,或者保存到其他位置
- javascript之DOM编程通过html元素的标签属性找节点
- servlet_3
- 【比赛】NOIP2018 货币系统
- 在CentOS 6.3中安装与配置cmake
- 六:python 对象类型详解二:字符串(下)
热门文章
- iOS---iPad开发及iPad特有的特技
- (转)淘淘商城系列——MyBatis分页插件(PageHelper)的使用以及商品列表展示
- windows测试物理网络
- 解决android的键盘弹出时,html页面的高度被压缩
- php简单实用的调试工具类
- 计算机内存数据存储基本原理----寄存器和RAM的电路基础
- spring cloud Bug之was unable to refresh its cache! status = Cannot execute request on any known server
- java_线程优先级
- 记录一次通过命令行方式来使用svn碰到的一系列问题
- 微信小程序UI组件库 iView Weapp快速上手