C++中的运算符重载

目录

1、运算符重载规则

2、运算符重载的两种形式

2.1、成员函数重载形式

2.2、普通的非成员函数重载形式


1、运算符重载规则

C++中可以重定义或重载大部分 C++ 内置的运算符。这样,我们就能使用自定义类型的运算符。

重载的运算符是带有特殊名称的函数,函数名是由关键字 operator 和其后要重载的运算符符号构

成的。与其他函数一样,重载运算符有一个返回类型和一个参数列表。

2、运算符重载的两种形式

2.1、成员函数重载形式

cpp 复制代码
Box operator+(const Box&);

成员函数重载形式中的重载函数一定要定义在成员函数中,而且形参只有 0 个或者 1 个,绝对不能写成下面 2.2 中两个形参的形式。第一个参数使用 this 指针传递参数。

cpp 复制代码
#include <iostream>
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 <<endl;

   // Box2 的体积
   volume = Box2.getVolume();
   cout << "Volume of Box2 : " << volume <<endl;

   // 把两个对象相加,得到 Box3
   Box3 = Box1 + Box2;

   // Box3 的体积
   volume = Box3.getVolume();
   cout << "Volume of Box3 : " << volume <<endl;

   return 0;
}

Volume of Box1 : 210

Volume of Box2 : 1560

Volume of Box3 : 5400

2.2、普通的非成员函数重载形式

cpp 复制代码
Box operator+(const Box&, const Box&);

若是选择普通的非成员函数重载形式,重载函数定义在类外,并且一定要传递两个参数。而且要注意,如果类的属性是私有的,则一定要在类中使用友元函数声明该重载函数可以访问类的私有属性,否则会发生编译错误,非成员函数不能访问类的私有属性。

cpp 复制代码
#include <iostream>
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 <<endl;

   // Box2 的体积
   volume = Box2.getVolume();
   cout << "Volume of Box2 : " << volume <<endl;

   // 把两个对象相加,得到 Box3
   Box3 = Box1 + Box2;

   // Box3 的体积
   volume = Box3.getVolume();
   cout << "Volume of Box3 : " << volume <<endl;

   return 0;
}

Volume of Box1 : 210

Volume of Box2 : 1560

Volume of Box3 : 5400

相关推荐
阳光阿盖尔2 分钟前
EasyExcel的基本使用——Java导入Excel数据
java·开发语言·excel
蔚一5 分钟前
Java设计模式—面向对象设计原则(三) -----> 依赖倒转原则DIP(完整详解,附有代码+案例)
java·开发语言·设计模式·intellij-idea·依赖倒置原则
liang899910 分钟前
SpringSecurity原理解析(七):权限校验流程
java·开发语言
LQS202010 分钟前
基于Python实现一个浪漫烟花秀
开发语言·python
QXH20000012 分钟前
数据结构—单链表
c语言·开发语言·数据结构
梅如你12 分钟前
python批量对遥感影像进行归一化与数据清洗
开发语言·python
imaima66613 分钟前
数据结构----栈和队列
开发语言·数据结构
sinat_2765225717 分钟前
C++中move的使用
开发语言·c++
Langneer18 分钟前
Qt 状态机编程,双层状态机,实现暂停恢复
开发语言·qt
搁浅°87919 分钟前
IO文件拷贝
java·开发语言