【C++学习手札】一文带你初识运算符重载

简介: 【C++学习手札】一文带你初识运算符重载

一、运算符重载基本概念

什么是运算符重载?

       运算符重载, 就是对已有的运算符重新进行定义, 赋予其另一种功能, 以适应不同

的数据类型。

       运算符重载(operator overloading)只是一种”语法上的方便”,也就是它只是另一种函

数调用的方式。

       在 c++中, 可以定义一个处理类的新运算符。 这种定义很像一个普通的函数定义,只是函数的名字由关键字 operator 及其紧跟的运算符组成。 差别仅此而已。 它像任何其他函数一样也是一个函数, 当编译器遇到适当的模式时, 就会调用这个函数。

运算符重载简要干货

       运算符重载的目的:简化操作 让已有的运算符 适应适应不同的数据类型。

       语法:函数的名字由关键字operator及其紧跟的运算符组成

       比如:重载+运算符 ==>     operator+ 重载=号运算     ==>     operator=

       注意:重载运算符 不要更改 运算符的本质操作(+是数据的相加 不要重载成相减)

栗子:(以下为重载了<<运算符的类)

class Data
{
  friend ostream& operator<<(ostream& out, Data& ob);//友元函数,经常与运算符重载搭配使用
private:
  int a;
  int b;
public:
  Data()
  {
    cout << "无参的构造函数" << endl;
    a = 0;
    b = 0;
  }
  Data(int a, int b) :a(a), b(b)
  {
    cout << "有参构造" << endl;
    //this‐>a = a;
      //this‐>b = b;
  }
  void showData(void)
  {
    cout << "a = " << a << ", b= " << b << endl;
  }
  ~Data()
  {
    cout << "析构函数函数" << endl;
  }
};
ostream& operator<<(ostream& out, Data& ob)
 {
   out << "a = " << ob.a << ", b = " << ob.b;
   return out;
 }

    解释:

       为了简化类中访问私有数据较为困难的问题,运用友元函数(下小点会提到)同重载运算符的结合,得以运用我们较为常用的<<直接输出数据。

可重载的运算符有哪些?

       几乎 C 中所有的运算符都可以重载, 但运算符重载的使用时相当受限制的。 特别是不能使用 C 中当前没有意义的运算符(例如用**求幂)不能改变运算符优先级, 不能改变运算符的参数个数。 这样的限制有意义, 否则, 所有这些行为产生的运算符只会混淆而不是澄清寓语意。

     一张图囊括~


二、前置知识-友元函数

什么是友元函数?

一句话概括:C++允许 友元 访问 私有数据。

友元函数的语法

friend+定义的函数

     注意: friend关键字只出现在声明处 其他类、类成员函数、全局函数都可声明为友元 友元函数不是类的成员,不带this指针 友元函数可访问对象任意成员属性,包括私有属性。

栗子: (创建一个房间类,你只准许你的朋友进入你的卧室,但是客厅是谁都可以进的)

class Room
   {
   //将goodGayVisit作为类的友元函数
     //goodGayVisit 访问 类中所有数据 但是 它不是类的成员
     friend void goodGayVisit(Room & room);
 private:
   string bedRoom;//卧室
 public:
   string sittingRoom;//客厅
 public: 
   Room()
   {
   this-> bedRoom = "卧室";
   this-> sittingRoom = "客厅";
   }
  };
 // 普通全局函数 作为 类的友元
 //好基友 访问 我的房间
 void goodGayVisit(Room & room)
 {
   cout << "好基友访问了你的" << room.sittingRoom << endl;
   cout << "好基友访问了你的" << room.bedRoom << endl;//ok
   }
 void test01()
 {
   Room myRoom;
   goodGayVisit(myRoom);
}

     friend在这里可以访问对象任意成员属性,包括私有属性。因此,本来不能访问的私有数据,在friend的情况下就可以访问了!结果如下:

       此为普通全局函数 作为 类的友元 。当然,也有类的某个成员函数 作为 另一个类的友元;一个类整体 作为 另一个类的友元等等。

       而我们的友元函数大多应用在重载运算符上!

     本文仅仅对友元函数做简单介绍,如果大家需要详解,请在评论区或者私信踢我一脚o(╯□╰)o,作者肯定会出一篇的!


三、运算符重载

运算符重载的语法

(根据自身改变的返回类型)operator + 重载的运算符(根据实际情况改变的传参)
  1. 函数声明:运算符重载是通过在类中定义特殊的成员函数来实现的。这些成员函数被称为运算符重载函数。例如,如果要重载"+"运算符,则需要在类中声明一个名为"operator+"的函数。
  2. 函数名:运算符重载函数的命名规则是以"operator"关键字开始,后面跟着要重载的运算符符号。例如,要重载"+“运算符,函数名应为"operator+”。
  3. 参数列表:运算符重载函数参数列表取决于所重载的运算符。例如,对于二元运算符如"+", “-”, “*”, “/“等,参数列表应包含一个额外的参数,表示右操作数。对于一元运算符如”++”, "– – "等,参数列表不需要额外的参数。
  4. 返回类型:运算符重载函数的返回类型取决于所重载的运算符。例如,对于"+"运算符,返回类型通常是所操作对象的类型。成员函数或友元函数:运算符重载函数可以作为类的成员函数或友元函数来定义。成员函数形式的运算符重载函数将使用对象本身作为左操作数,而友元函数形式的运算符重载函数将不使用任何对象。

一步一步带你实现运算符重载(以<<为例)

       注意:此代码未能实现重载 下文为对用cout来输出类的一个引入

#define _CRT_SECURE_NO_WARNINGS 01
#include <iostream>
#include<string.h>
 using namespace std;
 class Person
 {
 private:
   char* name;
   int num;
 public:
   Person(char* name, int num)
     {
     this-> name = new char[strlen(name) + 1];
     strcpy(this-> name, name);
     this-> num = num;
     cout << "有参构造" << endl;
     }
   //普通的成员函数
     void printPerson(void)
     {
     cout << "name = " << name << ", num = " << num << endl;
     }
   ~Person()
     {
     if (this-> name != NULL)
       {
       delete[] this-> name;
       this-> name = NULL;
       }
     cout << "析构函数" << endl;
     }
   };
 int main(int argc, char* argv[])
 {
   char arr[] = "lucy";
   Person ob1(arr, 18);
   //普通的成员函数 遍历信息
   //ob1.printPerson();
   //cout默认输出方式 无法识别 自定义对象 输出格式
  cout<<ob1<<endl;//err
  return 0;
 }

运行改代码,我们发现编译器报错!如下图:

        这个时候我们就需要对运算符进行重载了!

       那么问题又来了?如何重载运算符呢?根据上文所提到的语法,我们做出以下的操作:

       运用operator来重载<<运算符

 ostream& operator<<(ostream& out, Person& ob)//out=cout, ob =ob1
    {
    //重新实现 输出格式
      out << ob.name << ", " << ob.num;
      //每次执行为 返回值得到cout
      return out;
    }

     注意:ostream为cout的类型,定义ostream&为返回类型是为了作为起到链接的效果,如:

cout<<ob1<<ob2<<endl;ostream&返回out,然后再次被后面所调用,一直反复调用下去。

      然而,进行了运算符重载,就能实现我们想要的效果了吗?答案是不能,见下图:

        造成这样的原因是什么呢?还是类的封装问题,私有的数据不能被外界所访问!这时,我们就需要用到友元函数来帮助我们实现了!

于是,我们将operator<<设置成友元:

#define _CRT_SECURE_NO_WARNINGS 01
#include <iostream>
#include<string.h>
 using namespace std;
 class Person
 {
   //设置成友元函数 在函数内 访问Person类中的所有数据
    friend ostream & operator<<(ostream & out, Person & ob);
 private:
   char* name;
   int num;
 public:
   Person(char* name, int num)
     {
     this-> name = new char[strlen(name) + 1];
     strcpy(this-> name, name);
     this-> num = num;
     cout << "有参构造" << endl;
     }
   //普通的成员函数
     void printPerson(void)
     {
     cout << "name = " << name << ", num = " << num << endl;
     }
   ~Person()
     {
     if (this-> name != NULL)
       {
       delete[] this-> name;
       this-> name = NULL;
       }
     cout << "析构函数" << endl;
     }
   };
 ostream& operator<<(ostream& out, Person& ob)//out=cout, ob =ob1
    {
    //重新实现 输出格式
      out << ob.name << ", " << ob.num;
      //每次执行为 返回值得到cout
      return out;
    }
 int main(int argc, char* argv[])
 {
   char arr[] = "lucy";
   Person ob1(arr, 18);
   //普通的成员函数 遍历信息
   //ob1.printPerson();
   //cout默认输出方式 无法识别 自定义对象 输出格式
  cout<<ob1<<endl;//err
  return 0;
 }

实现效果如下:


运算符重载作为成员函数以及全局函数的实现(以+为例)

全局函数

       这里同上面的栗子大致一样,不过多叙述

#include <iostream>
 #include<string.h>
 using namespace std;
 class Person
 {
   //设置成友元函数 在函数内 访问Person类中的所有数据
   friend ostream & operator<<(ostream & out, Person & ob);
   friend Person operator+(Person & ob1, Person & ob2);
 private:
   char* name;
   int num;
 public:
   Person(){
     this-> name = NULL;
     this-> num = 0;
     cout << "无参构造" << endl;
     }
   Person(char* name, int num)
     {
     this-> name = new char[strlen(name) + 1];
     strcpy(this-> name, name);
     this-> num = num;
     cout << "有参构造" << endl;
     }
   //普通的成员函数
     void printPerson(void)
     {
     cout << "name = " << name << ", num = " << num << endl;
     }
   ~Person()
     {
     if (this-> name != NULL)
       {
       delete[] this-> name;
       this-> name = NULL;
       }
     cout << "析构函数" << endl;
     }
   };
 //全局函数作为友元 完成运算符重载<<
 ostream & operator<<(ostream & out, Person & ob)//out=cout, ob =ob1
 {
   //重新实现 输出格式
     out << ob.name << ", " << ob.num;
     //每次执行为 返回值得到cout
     return out;
   }
 //全局函数作为友元 完成运算符重载+
 Person operator+(Person & ob1, Person & ob2)//ob1 ob2
 { //name+name(字符串追加)
 char* tmp_name = new char[strlen(ob1.name) + strlen(ob2.name) + 1];
 strcpy(tmp_name, ob1.name);
 strcat(tmp_name, ob2.name);
 //num+num(数值相加)
 int tmp_num = ob1.num + ob2.num;
 Person tmp(tmp_name, tmp_num);
 //释放tmp_name的空间
 if (tmp_name != NULL)
 {
   delete[] tmp_name;
   tmp_name = NULL;
   }
 return tmp;
 }
 void test02()
 {
   char arr[] = "lucy";
   Person ob1(arr, 18);  
   char arr2[] = "bob";
   Person ob2(arr2, 19);
     cout << ob1 << endl;
    cout << ob2 << endl;
   //Person ob3 = operator+(ob1,ob2);
     Person ob3 = ob1 + ob2;
   cout << ob3 << endl;
   }
 int main(int argc, char* argv[])
 {
   test02();
   return 0;
 }
成员函数
#include <iostream>
 #include<string.h>
 using namespace std;
 class Person
 { //设置成友元函数 在函数内 访问Person类中的所有数据
 friend ostream & operator<<(ostream & out, Person & ob);
 private:
   char* name;
   int num;
 public:
   Person()
     {
     this-> name = NULL;
     this-> num = 0;
     cout << "无参构造" << endl;
     }
   Person(char* name, int num)
     {
     this-> name = new char[strlen(name) + 1];
     strcpy(this-> name, name);
     this-> num = num;
     cout << "有参构造" << endl;
     }
   //成员函数 完成运算符重载 ob1用this代替 ob2用参数ob代替
     Person operator+(Person & ob)
     {
     //this ==> &ob1
       //name+name(字符串追加)
     char* tmp_name = new char[strlen(this-> name) + strlen(ob.name) + 1];
     strcpy(tmp_name, this-> name);
     strcat(tmp_name, ob.name);
       //num+num(数值相加)
     int tmp_num = this-> num + ob.num;
     Person tmp(tmp_name, tmp_num);
       //释放tmp_name的空间
       if (tmp_name != NULL)
       {
       delete[] tmp_name;
       tmp_name = NULL;
       }
     return tmp;
     }
     //普通的成员函数
     void printPerson(void)
     {
     cout << "name = " << name << ", num = " << num << endl;
     }
   ~Person()
     {
     if (this-> name != NULL)
       {
       delete[] this-> name;
       this-> name = NULL;
       }
     cout << "析构函数" << endl;
     }
   };
 //全局函数作为友元 完成运算符重载<<
 ostream & operator<<(ostream & out, Person & ob)//out=cout, ob =ob1
 {
   //重新实现 输出格式
     out << ob.name << ", " << ob.num;
     //每次执行为 返回值得到cout
     return out;
   }
 void test03()
 {
   char arr[] = "lucy";
   char arr2[] = "bob";
   Person ob1(arr, 18);
   Person ob2(arr2, 19);
     //Person ob3 = ob1.operator+(ob2);
     Person ob3 = ob1 + ob2;
   cout << ob3 << endl;
   }
 int main(int argc, char* argv[])
 { test03();
 return 0;
 }

 在运算符重载运算符时,如果我们以成员函数的方式定义,则可以直接访问类中的数据,无需再使用友元函数来定义。因此我们在重载运算符时最好是以成员函数的方式重载!

四、++和--运算符重载 (重要、常用)

       不知道大家有没有一个疑惑如果我们实现前置+ +、后置+ +以及前置- -、后置--,运用operator时如何区分他们呢?

       此时,我们又要提到一个概念,当编译器看到++a(前置++),它就调用operator++(a),当编译器看到a++(后置++),它就会去调用operator++(a,int)。 - -也是同样的道理,具体实现如下:

具体实现用例

#include <iostream>
using namespace std;
 class Data
 {
   friend ostream & operator<<(ostream & out, Data & ob);
 private:
   int a;
   int b;
 public:
  Data()
     {
     cout << "无参的构造函数" << endl;
     a = 0;
     b = 0;
     }
   Data(int a, int b) :a(a), b(b)
     {
     cout << "有参构造" << endl;
     //this‐>a = a;
       //this‐>b = b;
       }
   void showData(void)
     {
     cout << "a = " << a << ", b= " << b << endl;
     }
   ~Data()
     {
     cout << "析构函数函数" << endl;
     }
     //成员函数 重载前置++ ++ob1 (先加 后使用)
     //编译器 默认识别 operator++(a) //但是a可以用this代替 从而化简 operator++()
     Data & operator++()//++ob1
     { //先加
     a++;//this‐>a = this‐>a +1
   b++;//this‐>b = this‐>b +1
   //后使用
     return *this;
   }
   //成员函数 重载后置++ ob1++ (先使用 后加)
     //编译器 默认识别 operator++(a,int) //但是a可以用this代替 从而化简 operator ++(int)
     Data & operator++(int)//ob1++
     {
     //先使用(备份加之前的值)
       static Data old = *this;
       //后加
      a++;
      b++;
       //返回备份值
       return old;
     }
     //重载前置‐‐ ‐‐ob3
     //编译器 默认识别 operator++(a) //但是a可以用this代替 从而化简 operator‐‐()
     Data & operator--()
     {
     //先减
      a--;
      b--;
       //后使用(返回)
       return *this;
     }
     //重载后‐‐ ob4‐‐
     //编译器 默认识别 operator++(a,int) //但是a可以用this代替 从而化简 operator++(int)
     Data & operator--(int)
     {
     //先使用
       static Data old = *this;
       //再减
      a--;
      b--;
       return old;
     }
     };
 //普通全局函数 作为类的友元 重载<<运算符
 ostream & operator<<(ostream & out, Data & ob)
 {
   out << "a = " << ob.a << ", b = " << ob.b;
   return out;
   }
 void test01()
 {
   Data ob1(10, 20);
   ob1.showData();
     //重载<<直接输出自定义对象的值
     //operator<<(cout,ob1);
     cout << ob1 << endl;
     //成员函数 重载 ++运算符
     cout << ++ob1 << endl;
     Data ob2(10, 20);
   cout << ob2++ << endl;
   cout << ob2 << endl;
     //成员函数 重载 ‐‐运算符
     Data ob3(10, 20);
   cout << "ob3 " << ob3 << endl;
   cout << --ob3 << endl;
     Data ob4(10, 20);
   cout << "ob4 " << ob4 << endl;
   cout << ob4-- << endl;
   cout << "ob4 " << ob4 << endl; 
     }
 int main(int argc, char* argv[])
 {
   test01();
   return 0;
 }

  效果如下:


               感谢你耐心的看到这里ღ( ´・ᴗ・` )比心,如有哪里有错误请踢一脚作者o(╥﹏╥)o!  

相关文章
|
20小时前
|
程序员 C++
C++程序中的运算符重载
C++程序中的运算符重载
8 2
|
4天前
|
测试技术 C++
C++|运算符重载(3)|日期类的计算
C++|运算符重载(3)|日期类的计算
|
4天前
|
C语言 C++
C++|运算符重载(2)|运算符重载的方法与规则
C++|运算符重载(2)|运算符重载的方法与规则
|
4天前
|
编译器 C++
C++|运算符重载(1)|为什么要进行运算符重载
C++|运算符重载(1)|为什么要进行运算符重载
|
6天前
|
编译器 C++
【C++】继续学习 string类 吧
首先不得不说的是由于历史原因,string的接口多达130多个,简直冗杂… 所以学习过程中,我们只需要选取常用的,好用的来进行使用即可(有种垃圾堆里翻美食的感觉)
9 1
|
6天前
|
算法 安全 程序员
【C++】STL学习之旅——初识STL,认识string类
现在我正式开始学习STL,这让我期待好久了,一想到不用手撕链表,手搓堆栈,心里非常爽
17 0
|
6天前
|
存储 安全 测试技术
【C++】string学习 — 手搓string类项目
C++ 的 string 类是 C++ 标准库中提供的一个用于处理字符串的类。它在 C++ 的历史中扮演了重要的角色,为字符串处理提供了更加方便、高效的方法。
18 0
【C++】string学习 — 手搓string类项目
|
6天前
|
编译器 C语言 C++
【C++从练气到飞升】05---运算符重载(二)
【C++从练气到飞升】05---运算符重载(二)
|
6天前
|
编译器 C++
【C++从练气到飞升】05---运算符重载(一)
【C++从练气到飞升】05---运算符重载(一)
|
6天前
|
编译器 C++
【C++】类与对象(运算符重载、const成员、取地址重载)
【C++】类与对象(运算符重载、const成员、取地址重载)
15 2