目录
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
-
相关阅读:
LLVM(6)ORC实例分析:Transform in cpp
浅析资源调度框架YARN
Docker学习——容器
UE4C++学习篇(十八)-- SceneCaptureComponent组件处理小地图
Ubuntu 20.04 LTS配置JDK、Git
八数码—unordered哈希表即记录步骤—A*算法—曼哈顿距离—BFS
LLM实战(一)| 使用LLM抽取关键词
Java高级编程-----网络编程
小程序样式问题
Hypertable install of rhel6.0
-
原文地址:https://blog.csdn.net/qq_51691366/article/details/133757281