zoukankan      html  css  js  c++  java
  • 运算符重载

    一、运算符重载

          定义格式:

                operator 运算符 ();

          说明:

                1. 运算符重载是通过创建运算符函数operator()来实现的;

                2. 函数operator()可以是它将要操作的类的成员,也可以不是类的成员,但非成员operator()函数通常是类的友元函数;

                3. 一般来讲,运算符重载的功能应当与原有的功能相类似;

                4. C++语言中只能重载原先已有定义的运算符;

                5. 下列运算符不能被重载: 类属关系运算符“ . ”, 成员指针运算符“ * ”, 作用域分辨符“ :: ”, sizeof运算符和 

                    三目运算符“ ?: ”;

                6. 不能改变运算符的操作个数、原有的优先级、原有的结合特性、对预定义类型数据的操作方式;

                7. 运算符重载函数operator运算符()可以返回任何类型,甚至可以是void类型,但通常返回类型与它所操作的类的类型相

                    同,这样可使重载运算符用在复杂的表达式中;

                8. c++编译器根据参数的个数和类型来决定调用哪个重载函数,因此可为同一个运算符定义几个运算符重载函数来进行不同的

                    操作。 

    二、友元运算符函数

            类内部声明格式:

                  friend 返回类型 operator 运算符(形参表);

            类外部定义格式:

                  返回类型 operator 运算符(形参表)

                  {

                            函数体

                  }

            双目运算符友元函数调用方法:

                  对象1名 运算符 对象2名;   //隐式调用               或             operator 运算符 (对象1名,对象2名);     //显式调用

            单目运算符友元函数调用方法:

                  运算符 对象名;   //隐式调用            或               operator 运算符 (对象名);     //显式调用

            说明:

                  1. 若友元运算符函数重载的是双目运算符,则参数表中有两个操作数;若重载的是单目运算符,则参数表中只有一个操

                      作数;

                  2. 有时,在函数返回的时候,可以直接用类的构造函数来生成一个临时对象,而不对该对象进行命名; 

                  3. 友元函数没有this指针,不能引用this指针所指的对象;这个函数采用对象参数通过传值的方法传递参数的;

                  4. 不能用友元函数重载的运算符是: =, (), [], -> ;

    例子:

    #include <iostream.h>    
      
    class KComplex
    {
    private:
        double real;
        double imag;
    public:
        KComplex(double r = 0, double i = 0)
        {
            real = r;
            imag = i;
        }
    
        void Print()
        {
            cout<<real;
            if (imag > 0)
            {
                cout<<" + ";
            }
            cout<<imag<<"i"<<endl;
        }
    
        friend KComplex operator + (KComplex, KComplex);      //用友元双目运算符函数重载运算符“+”
        friend KComplex operator - (KComplex, KComplex);      //用友元双目运算符函数重载运算符“-”
        friend KComplex operator * (KComplex, KComplex);      //用友元双目运算符函数重载运算符“*”
        friend KComplex operator / (KComplex, KComplex);      //用友元双目运算符函数重载运算符“/”
        friend KComplex operator ++ (KComplex);               //用友元单目运算符函数重载运算符“++”(前缀)
    };
    
    KComplex operator + (KComplex co1, KComplex co2)          //重载运算符“+”的实现
    {//在函数返回时,可直接用类的构造函数来生成一个临时对象,
     //而不对该对象命名,这样是直接将一个无名临时对象创建到主调函数中,效率提高了很多
        return KComplex(co1.real + co2.real, co1.imag + co2.imag);
    }
    
    KComplex operator - (KComplex co1, KComplex co2)          //重载运算符“-”的实现
    {
        return KComplex(co1.real - co2.real, co1.imag - co2.imag);
    }
    
    KComplex operator * (KComplex co1, KComplex co2)          //重载运算符“*”的实现
    {
        double re, im;
        re = co1.real * co2.real - co1.imag * co2.imag;
        im = co1.real * co2.imag + co1.imag * co2.real;
        return KComplex(re, im);
    }
    
    KComplex operator / (KComplex co1, KComplex co2)          //重载运算符“/”的实现
    {
        double re, im, te;
        te = co1.real * co1.real + co1.imag * co1.imag;
        re = (co1.real * co2.real + co1.imag * co2.imag) / te;
        im = (co2.imag * co1.real - co2.real * co1.imag) / te;
        return KComplex(re, im);
    }
    
    KComplex operator ++ (KComplex co)                       //重载运算符“++”的实现
    {    
        return KComplex(++co.real, ++co.imag);
    }
    
    int main()
    {
        KComplex com1(3, 4), com2(5, -10), total;
    
        total = com1 + com2;
        cout<<"com1 + com2 = ";
        total.Print();
    
        total = operator - (com1, com2);
        cout<<"com1 - com2 = ";
        total.Print();
    
        total = com1 * com2;
        cout<<"com1 * com2 = ";
        total.Print();
    
        total = com1 / com2;
        cout<<"com1 / com2 = ";
        total.Print();
    
        total = ++com1;
        cout<<"++com1 = ";
        total.Print();
    
        return 0;
    }

    程序结果:

                         com1 + com2 = 8-6i

                         com1 - com2 = -2 + 14i

                         com1 * com2 = 55-10i

                         com1 / com2 = -1-2i

                         ++com1 = 4 + 5i

    三、成员运算符函数

            类内部声明格式:

                  返回类型 operator 运算符 (形参表);

            类外部定义格式:

                  返回类型 类名::operator 运算符 (形参表)

                 {

                          函数体

                 }

           双目运算符成员函数调用方法:

                 对象1名 运算符 对象2名;   //隐式调用              或                对象1名.operator 运算符 (对象2名);     //显式调用

           单目运算符成员函数调用方法:

                 运算符 对象名;   //隐式调用               或              对象名.operator 运算符 ();     //显式调用              

           说明:

                 1. 在成员运算符函数的形参表中,若运算符是单目的,则参数表为空;若运算符是双目运算符,则参数表中有一个操作数;

                 2. 对双目运算符成员函数,其形参表中的参数作为运算符的右操作数,此时当前对象作为运算符的左操作数,它是通过this指

                     针隐含地传递给函数的。

    例子:

    #include <iostream>    
    using namespace std; 
      
    class KComplex
    {
    private:
        double real;
        double imag;
    public:
        KComplex(double r = 0, double i = 0)
        {
            real = r;
            imag = i;
        }
        double GetReal()
        {
            return real;
        }
        double GetImag()
        {
            return imag;
        }
        KComplex operator +  (KComplex);          //双目运算符的成员函数在类内部声明格式
        KComplex operator ++  ();                 //单目运算符的成员函数在类内部声明格式
    };
    
    KComplex KComplex::operator + (KComplex col)      //双目运算符的成员函数在类外部定义格式
    {
        return KComplex(col.real + real, col.imag + imag);
    }
    
    KComplex KComplex::operator ++ ()                 //单目运算符的成员函数在类外部定义格式
    {
        return KComplex(++real, ++imag);
    }
    
    int main()
    {
        KComplex com1(1.1,2.2), com2(3.3,4.4), total1, total2, total3, total4;
        total1 = com1.operator+ (com2);           //双目运算符成员函数的调用
        total2 = com1 + com2;
        cout<<"real1 = "<<total1.GetReal()<<" "<<"imag1 = "<<total1.GetImag()<<endl;
        cout<<"real2 = "<<total2.GetReal()<<" "<<"imag2 = "<<total2.GetImag()<<endl;
        total3 = com1.operator++ ();              //单目运算符成员函数的调用
        total4 = ++com1;
        cout<<"real1 = "<<total3.GetReal()<<" "<<"imag1 = "<<total3.GetImag()<<endl;
        cout<<"real2 = "<<total4.GetReal()<<" "<<"imag2 = "<<total4.GetImag()<<endl;
        return 0;
    }

    程序结果:

                      real1 = 4.4 imag1 = 6.6

                      real2 = 4.4 imag2 = 6.6

                      real1 = 2.1 imag1 = 3.2

                      real2 = 3.1 imag2 = 4.2

  • 相关阅读:
    【Vue】状态管理
    【Vue】路由
    【Vue】组件
    【Vue】基础(数据 & 计算属性 & 方法)
    【Vue】基础(虚拟DOM & 响应式原理)
    【Vue】基础(生命周期 & 常用指令)
    【Vue】搭建开发环境
    【Mongodb】事务
    【Mongodb】视图 && 索引
    【Mongodb】聚合查询 && 固定集合
  • 原文地址:https://www.cnblogs.com/MrYuan/p/5332251.html
Copyright © 2011-2022 走看看