目录
C++中可以重定义或重载大部分 C++ 内置的运算符。这样,我们就能使用自定义类型的运算符。
重载的运算符是带有特殊名称的函数,函数名是由关键字 operator 和其后要重载的运算符符号构
成的。与其他函数一样,重载运算符有一个返回类型和一个参数列表。
Box operator+(const Box&);
成员函数重载形式中的重载函数一定要定义在成员函数中,而且形参只有 0 个或者 1 个,绝对不能写成下面 2.2 中两个形参的形式。第一个参数使用 this 指针传递参数。
- #include
- using namespace std;
-
- class Box
- {
- public:
-
- double getVolume(void)
- {
- return length * breadth * height;
- }
- void setLength( double len )
- {
- length = len;
- }
-
- void setBreadth( double bre )
- {
- breadth = bre;
- }
-
- void setHeight( double hei )
- {
- height = hei;
- }
- // 重载 + 运算符,用于把两个 Box 对象相加,成员运算符重载
- Box operator+(const Box& b)
- {
- Box box;
- box.length = this->length + b.length;
- box.breadth = this->breadth + b.breadth;
- box.height = this->height + b.height;
- return box;
- }
- private:
- double length; // 长度
- double breadth; // 宽度
- double height; // 高度
- };
- int main( )
- {
- Box Box1; // 声明 Box1,类型为 Box
- Box Box2; // 声明 Box2,类型为 Box
- Box Box3; // 声明 Box3,类型为 Box
- double volume = 0.0; // 把体积存储在该变量中
-
- // Box1 详述
- Box1.setLength(6.0);
- Box1.setBreadth(7.0);
- Box1.setHeight(5.0);
-
- // Box2 详述
- Box2.setLength(12.0);
- Box2.setBreadth(13.0);
- Box2.setHeight(10.0);
-
- // Box1 的体积
- volume = Box1.getVolume();
- cout << "Volume of Box1 : " << volume <
-
- // Box2 的体积
- volume = Box2.getVolume();
- cout << "Volume of Box2 : " << volume <
-
- // 把两个对象相加,得到 Box3
- Box3 = Box1 + Box2;
-
- // Box3 的体积
- volume = Box3.getVolume();
- cout << "Volume of Box3 : " << volume <
-
- return 0;
- }
Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400
2.2、普通的非成员函数重载形式
Box operator+(const Box&, const Box&);
若是选择普通的非成员函数重载形式,重载函数定义在类外,并且一定要传递两个参数。而且要注意,如果类的属性是私有的,则一定要在类中使用友元函数声明该重载函数可以访问类的私有属性,否则会发生编译错误,非成员函数不能访问类的私有属性。
- #include
- using namespace std;
-
- class Box
- {
- friend Box operator+(const Box& box1, const Box& box2);
- public:
-
- double getVolume(void)
- {
- return length * breadth * height;
- }
- void setLength( double len )
- {
- length = len;
- }
-
- void setBreadth( double bre )
- {
- breadth = bre;
- }
-
- void setHeight( double hei )
- {
- height = hei;
- }
- private:
- double length; // 长度
- double breadth; // 宽度
- double height; // 高度
- };
- //类的非成员运算符重载
- Box operator+(const Box& box1, const Box& box2){
- Box box;
- box.length = box1.length + box2.length;
- box.breadth = box1.breadth + box2.breadth;
- box.height = box1.height + box2.height;
- return box;
- }
-
- int main( )
- {
- Box Box1; // 声明 Box1,类型为 Box
- Box Box2; // 声明 Box2,类型为 Box
- Box Box3; // 声明 Box3,类型为 Box
- double volume = 0.0; // 把体积存储在该变量中
-
- // Box1 详述
- Box1.setLength(6.0);
- Box1.setBreadth(7.0);
- Box1.setHeight(5.0);
-
- // Box2 详述
- Box2.setLength(12.0);
- Box2.setBreadth(13.0);
- Box2.setHeight(10.0);
-
- // Box1 的体积
- volume = Box1.getVolume();
- cout << "Volume of Box1 : " << volume <
-
- // Box2 的体积
- volume = Box2.getVolume();
- cout << "Volume of Box2 : " << volume <
-
- // 把两个对象相加,得到 Box3
- Box3 = Box1 + Box2;
-
- // Box3 的体积
- volume = Box3.getVolume();
- cout << "Volume of Box3 : " << volume <
-
- return 0;
- }
Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400
-
相关阅读:
计算机二级WPS 选择题(模拟和解析五)
541. 反转字符串 II
建筑特种工附着式升降脚手架工种试题及答案
人工智能模型的微小单元
Swin-Transformer 从数据尺度变换角度解析
Java中的异步日志记录与性能优化
LeetCode-剑指22-链表中倒数第k个节点
Selenium获取本地已打开的浏览器页面进行跟踪和自定义日志记录
必不可少的UI组件二——组件库开发的基础知识(工程化篇)
ARM/Linux嵌入式面经(六):华为【共四面】
-
原文地址:https://blog.csdn.net/qq_51691366/article/details/133757281