news 2026/4/16 17:07:54

3.其他重载运算符

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
3.其他重载运算符

3.其他重载运算符

重载+, -, *

mytime2.h

#ifndef __MYTIME2_H__ #define __MYTIME2_H__ ​ #include <iostream> ​ using namespace std; ​ class Time { private: int hours; int minutes; public: Time(); Time(int h, int m = 0); void AddMin(int m); void AddHr(int h); void Reset(int h = 0, int m = 0); Time operator+(const Time &t) const; Time operator-(const Time &t) const; Time operator*(double mult) const; void Show() const; }; ​ #endif ​

mytime2.cpp

#include "mytime2.h" ​ Time::Time() { hours = minutes = 0; } ​ Time::Time(int h, int m) { hours = h; minutes = m; } ​ void Time::AddMin(int m) { minutes += m; hours += minutes / 60; minutes %= 60; } ​ void Time::AddHr(int h) { hours += h; } ​ void Time::Reset(int h, int m) { hours = h; minutes = m; } ​ Time Time::operator+(const Time &t) const { Time sum; ​ sum.minutes = minutes + t.minutes; sum.hours = hours + t.hours + sum.minutes / 60; sum.minutes %= 60; ​ return sum; } ​ Time Time::operator-(const Time &t) const { Time diff; ​ int tot1, tot2; tot1 = hours * 60 + minutes; tot2 = t.hours * 60 + t.minutes; diff.hours = (tot1 - tot2) / 60; diff.minutes = (tot1 - tot2) % 60; ​ return diff; } ​ Time Time::operator*(double mult) const { Time result; ​ long totalminutes = hours*mult*60 + minutes*mult; result.hours = totalminutes / 60; result.minutes = totalminutes % 60; ​ return result; } ​ void Time::Show() const { cout << hours << " hours, " << minutes << " minutes." << endl; } ​

usetime2.cpp

#include <iostream> #include "mytime2.h" ​ using namespace std; ​ int main(void) { Time coding(4, 35); Time fixing(2, 47); Time total; Time Planning; Time diff; Time adjusted; ​ cout << "coding time = "; coding.Show(); ​ cout << "fixing time = "; fixing.Show(); ​ total = coding + fixing; total.Show(); ​ diff = coding - fixing; diff.Show(); ​ adjusted = coding * 1.5;//左侧的操作数应是调用对象,coding需要在1.5前面 adjusted.Show(); ​ return 0; } ​

.

加法和减法运算符都结合两个Time 值,而乘法运算符将一个 Time 值与一个 double值结合在一起。这限制了该运算符的使用方式。记住,左侧的操作数是调用对象。也就是说,下面的语句:A=B * 2.75; 将被转换为下面的成员函数调用: A=B.operator * (2.75); 但下面的语句又如何呢? A=2.75 * B;//cannot correspond to a member function 从概念上说,2.75 * B应与B*2.75相同,但第一个表达式不对应于成员函数,因为 2.75不是 Time 类型的对象。记住,左侧的操作数应是调用对象,但2.75不是对象。因此,编译器不能使用成员函数调用来替换该表达式。

友元函数解决非成员函数不能直接访问类的私有数据

//创建友元函数的第一步是将其原型放在类声明中,并在原型声明前加上关键字friend: friend Time operator*(double m,const Time &t);//goes in class declaration

该原型意味着下面两点: 1.虽然 operator *()函数是在类声明中声明的,但它不是成员函数,因此不能使用成员运算符来调用;

2.虽然 operator *()函数不是成员函数,但它与成员函数的访问权限相同。

第二步是编写函数定义。因为它不是成员函数,所以不要使用Time限定符。另外,不要在定义中使用关键字friend。

将来在写非成员函数的时候,可以将其写成友元函数。

友元函数重载 *

mytime2.h

#ifndef __MYTIME3_H__ #define __MYTIME3_H__ ​ #include <iostream> ​ using namespace std; ​ class Time { private: int hours; int minutes; public: Time(); Time(int h, int m = 0); void AddMin(int m); void AddHr(int h); void Reset(int h = 0, int m = 0); Time operator+(const Time &t) const; Time operator-(const Time &t) const; Time operator*(double mult) const; friend Time operator*(double mult, const Time &t);//友元函数重载 * void Show() const; }; ​ //Time operator*(double m, const Time &t);//定义成一个普通的函数,无法使用类中的private。 ​ #endif ​

mytime2.cpp

#include "mytime3.h" ​ Time::Time() { hours = minutes = 0; } ​ Time::Time(int h, int m) { hours = h; minutes = m; } ​ void Time::AddMin(int m) { minutes += m; hours += minutes / 60; minutes %= 60; } ​ void Time::AddHr(int h) { hours += h; } ​ void Time::Reset(int h, int m) { hours = h; minutes = m; } ​ Time Time::operator+(const Time &t) const { Time sum; ​ sum.minutes = minutes + t.minutes; sum.hours = hours + t.hours + sum.minutes / 60; sum.minutes %= 60; ​ return sum; } ​ Time Time::operator-(const Time &t) const { Time diff; ​ int tot1, tot2; tot1 = hours * 60 + minutes; tot2 = t.hours * 60 + t.minutes; diff.hours = (tot1 - tot2) / 60; diff.minutes = (tot1 - tot2) % 60; ​ return diff; } ​ Time Time::operator*(double mult) const { Time result; ​ long totalminutes = hours*mult*60 + minutes*mult; result.hours = totalminutes / 60; result.minutes = totalminutes % 60; ​ return result; } ​ void Time::Show() const { cout << hours << " hours, " << minutes << " minutes." << endl; } ​ //友元函数重载 * Time operator*(double mult, const Time &t) { Time result; ​ long totalminutes = t.hours*mult*60 + t.minutes*mult; result.hours = totalminutes / 60; result.minutes = totalminutes % 60; ​ return result; } ​ /* Time operator*(double mult, const Time &t) { return t * mult; } */ ​

usetime2.cpp

#include <iostream> #include "mytime3.h" ​ using namespace std; ​ int main(void) { Time coding(4, 35); Time fixing(2, 47); Time total; Time Planning; Time diff; Time adjusted; ​ cout << "coding time = "; coding.Show(); ​ cout << "fixing time = "; fixing.Show(); ​ total = coding + fixing; total.Show(); ​ diff = coding - fixing; diff.Show(); ​ adjusted = coding * 1.5; adjusted.Show(); ​ adjusted = 1.5 * coding;//友元函数重载 *, 等价于operator*(1.5, coding) adjusted.Show(); ​ return 0; } ​

.

Time Time::operator*(double mult) constfriend Time operator*(double mult, const Time &t)这两个重载*的函数有什么区别?


✅ 一句话总结:

一个是成员函数,只能写成t * 2.0; 一个是友元函数,支持2.0 * t这种写法。


🔍 详细对比:

特性成员函数版本友元函数版本
定义方式Time Time::operator*(double mult) constfriend Time operator*(double m, const Time &t)
调用方式time * 2.02.0 * time
参数个数1 个显式参数(this是隐式参数)2 个显式参数
访问私有成员✅ 可以(因为是成员)✅ 可以(因为是友元)
是否支持交换律❌ 不支持(只能time * 2.0✅ 支持(可以2.0 * time

✅ 举个例子:

Time t(1, 30); // 1小时30分钟 ​ Time a = t * 2.0; // ✅ 成员函数版本 Time b = 2.0 * t; // ✅ 友元函数版本(没有它就不行)

✅ 总结一句话:

成员函数只能处理对象 * 数字友元函数才能处理数字 * 对象,实现真正的交换律。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/16 11:06:22

风扇精准控制终极指南:告别频繁启停,实现静音高效散热

风扇精准控制终极指南&#xff1a;告别频繁启停&#xff0c;实现静音高效散热 【免费下载链接】FanControl.Releases This is the release repository for Fan Control, a highly customizable fan controlling software for Windows. 项目地址: https://gitcode.com/GitHub_…

作者头像 李华
网站建设 2026/4/16 13:03:06

Python脚本打包EXE工具 v1.0:智能小巧高颜值

基于 Pyinstaller 内核开发的 Python 脚本打包 EXE 工具 v1.0&#xff0c;是一款专为普通用户打造的高颜值打包软件。它不仅解决了传统打包过程中的权限报错问题&#xff0c;还支持智能分析脚本依赖、极限压缩文件体积&#xff0c;无需复杂代码操作&#xff0c;就能快速将 Pyth…

作者头像 李华
网站建设 2026/4/16 11:03:35

45、Linux技术全解析:从基础符号到安全防护

Linux技术全解析:从基础符号到安全防护 1. 符号与数值基础 在Linux系统中,一些特殊符号有着重要的意义。例如,“.”代表当前目录,“..”代表父目录,“/”既表示根目录,也在文件系统组织中起到关键作用。这些符号是我们在操作文件和目录时的基础,理解它们能帮助我们更高…

作者头像 李华
网站建设 2026/4/16 11:11:35

LaTeX公式转图片工具:3步搞定专业数学公式排版

LaTeX公式转图片工具&#xff1a;3步搞定专业数学公式排版 【免费下载链接】latex2image-web LaTeX to image converter with web UI using Node.js / Docker 项目地址: https://gitcode.com/gh_mirrors/la/latex2image-web 还在为数学公式排版而烦恼吗&#xff1f;LaTe…

作者头像 李华
网站建设 2026/4/16 16:11:37

你真的看懂Buildx日志了吗?90%开发者忽略的3大关键点

第一章&#xff1a;你真的看懂Buildx日志了吗&#xff1f;当你在CI/CD流水线中运行Docker Buildx构建多架构镜像时&#xff0c;控制台输出的日志往往包含大量信息&#xff0c;但大多数开发者只是关注“是否成功”&#xff0c;而忽略了其中的关键细节。理解Buildx日志结构&#…

作者头像 李华
网站建设 2026/4/16 11:02:12

论文AI率太高怎么办?4个方法帮你快速降到个位数

马上就要截稿了&#xff0c;很多同学此时的心情大概是&#xff1a;查重率过了&#xff0c;以为万事大吉&#xff0c;结果一查AI率直接炸了 。 虽然论文是你一个个字敲出来的&#xff08;或者真的借鉴了一点点&#xff09;&#xff0c;但只要AI检测报告上显示红色预警&#xff…

作者头像 李华