C++之运算符的重载

前言

        运算符重载是特殊的函数重载,必须定义一个函数,并通知C++编译器,当遇到该重载的运算符时调用此函数。这个函数叫做运算符重载函数。


一、可重载运算符

  • C++中,只能对已有的运算符进行重载,不可自行定义新运算符;
  • C++中大部分运算符可以重载,除了以下运算符:

.:成员访问运算符

.*, ->*:成员指针访问运算符

:::域运算符

sizeof:长度运算符

?::条件运算符

#: 预处理符号

 二、重载运算符的方式

有以下两种方式

1.用友元函数(非成员函数)重载运算符

2.用成员函数重载运算符

只能通过非成员函数重载运算符的:

1.>>输入运算符

2.<<输出运算符

(操作数不是本类对象,是istream或ostream)

只能通过成员函数重载的:

=,() ,[] , - , ->

(涉及修改对象内部状态)

三、重载运算符的格式

Box operator+(const Box&);

包含返回类型(Box),重载运算符(+),传参(Box&)

为什么使用 const&传参:

1.通过引用传递,避免不必要的拷贝,提高运行效率

2.const 表示参数只读,不可被修改,防止函数内部意外修改传入的对象

四、重载一元运算符

下面给出示例

成员函数定义:

complex operator-()
{
    complex temp;
    temp.real=real*-1;
    temp.imag=imag*-1;
    return temp;
}

友元函数定义:

friend Complex operator-(const Complex& c);

Complex operator-(const Complex& c) {
    Complex temp;
    temp.real = -c.real; 
    temp.imag = -c.imag; 
    return temp;
}
  • 自增++和自减--运算符

作为前缀和后缀的语法格式不同,下面以++运算符为例

//前缀自增(先自增后返回值)
Complex operator ++() {
    ++i;
    ++j;
    return *this;
}


//后缀自增 需要增加int参数,用于区分(先返回当前值,再自增)
Complex operator ++(int) {
    Complex temp =*this;
    ++*this;
    return temp;
}
  • 重载下标[]运算符

用于自定义类对象的元素访问方式,应被重载为类的成员函数

int& operator[](int x)
{
    if(x<0||x>=count)
    {
        cout<<"数组越界\n";
        exit(0);
    }
    else return p[x];
}

五、重载二元运算符

二元运算符需要两个参数,当作为成员函数重载时,可只从外部传入一个参数

下面以+运算符为例(-,*,/等运算符同理)

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;
   }
  • 重载赋值(=)运算符

 MyClass& operator=(const MyClass& other) {
        if (this != &other) {  // 防止自赋值
            value = other.value;
        }
        return *this;  // 返回当前对象的引用,支持链式赋值
    }

重载赋值运算符时,需要注意

1.利用if语句判断,防止自赋值

2.返回类型为当前对象的引用,以支持链式赋值

六、重载关系运算符

C++ 语言支持各种关系运算符( < 、 > 、 <= 、 >= 、 == 等等)的重载,下面以重载<运算符为例

 bool operator <(const Distance& d)
      {
         if(feet < d.feet)
         {
            return true;
         }
         if(feet == d.feet && inches < d.inches)
         {
            return true;
         }
         return false;
      }

注意返回类型为布尔类型

七、重载输入输出运算符

流提取运算符 >> 和流插入运算符 << 重载时需要重载为友元函数

#include <iostream>
using namespace std;
 
class Distance
{
   private:
      int feet;             // 0 到无穷
      int inches;           // 0 到 12
   public:
      // 所需的构造函数
      Distance(){
         feet = 0;
         inches = 0;
      }
      Distance(int f, int i){
         feet = f;
         inches = i;
      }
      friend ostream &operator<<( ostream &out, 
                                       const Distance &D )
      { 
         out << "F : " << D.feet << " I : " << D.inches;
         return output;            
      }
 
      friend istream &operator>>( istream  &in, Distance &D )
      { 
         in >> D.feet >> D.inches;
         return in;            
      }
};
int main()
{
   Distance D1(11, 10), D2(5, 11), D3;
 
   cout << "Enter the value of object : " << endl;
   cin >> D3;
   cout << "First Distance : " << D1 << endl;
   cout << "Second Distance :" << D2 << endl;
   cout << "Third Distance :" << D3 << endl;
 
 
   return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值