zoukankan      html  css  js  c++  java
  • C++:运算符重载函数之成员运算符重载函数

    5.2.3 成员运算符重载函数

    在C++中可以把运算符重载函数定义为某个类的成员函数,称之为成员运算符重载函数。

    1. 定义成员运算符重载函数的语法形式
    (1)在类的内部,定义成员运算符重载函数的格式如下:
    函数类型 operator 运算符(形参表)
    {
           函数体
    }

    (2)成员运算符重载函数也可以在类中声明成员函数的原型,在类外定义。

    在类的内部,声明成员运算符重载函数原型的格式如下:
    class X{
          ...
          函数类型 operator运算符(参数表);
    };
    在类的外部定义,定义成员运算符重载函数原型的格式如下:
    函数类型 operator运算符(参数表)
    {
          函数体
    }

    其中,X是友元函数的运算符重载函数所在类的类名;函数类型指定了成员运算符函数的返回值类型;operator是定义运算符重载函数的关键字;运算符即是要重载的运算符名称,必须是C++中可重载的运算符;形参表中给出重载运算符所需要的参数和类型。由于成员运算符重载函数是该类的成员函数,所以在类外定义时必须缀上类名。

    注意:在成员运算符重载函数的形参表中,若运算符是单目的,则参数表为空;若运算符是双目的,则参数表中有一个操作数。

    2. 双目运算符重载

    对双目运算符而言,成员运算符重载函数的形参表中仅有一个参数,它作为运算符的右操作数。另一个操作数(左操作数)是隐含的,是该类的当前对象,它是通过this指针隐含地传递给函数的。例如

    calss X{
           ...
           int operator+(X a);
           ...
    };
    在类中声明重载"+"的成员运算符重载函数,返回类型为int,它具有两个操作数,一个是当前对象,另一个是类X的对象a。

    例5.5 用成员运算符重载函数进行复数的运算。
    加法:(a+bi)+(c+di)=(a+c)+(b+d)i
    减法:(a-bi)+(c-di)=(a-c)+(b-d)i
    乘法:(a-bi)*(c-di)=(ac-bd)+(ad+bc)i
    除法:(a-bi)/(c-di)=(a+bi)*(c-di)/(c*c+d*d)

    #include<iostream>
    using namespace std;
    class Complex{
      public:
        Complex(){};
        Complex(double r,double i)
        {
         real = r;
         imag = i;
        }
        void print();
        Complex operator+(Complex co);  //声明运算符+的重载函数 
        Complex operator-(Complex co);  //声明运算符-的重载函数
        Complex operator*(Complex co);  //声明运算符*的重载函数
        Complex operator/(Complex co);  //声明运算符/的重载函数
      private:
        double real;//复数的实部 
        double imag;//复数的虚部 
    };
    Complex Complex::operator+(Complex co)  //定义运算符+的重载函数 
    {
     Complex temp;
     temp.real = real+co.real;
     temp.imag = imag+co.imag;
     return temp;    
    }
    Complex Complex::operator-(Complex co)  //定义运算符-的重载函数
    {
     Complex temp;
     temp.real = real-co.real;
     temp.imag = imag-co.imag;
     return temp;  
    }  
    Complex Complex::operator*(Complex co)  //定义运算符*的重载函数
    {
     Complex temp;
     temp.real = real*co.real-imag*co.imag;
     temp.imag = real*co.imag+imag*co.real;
     return temp;
    }
    Complex Complex::operator/(Complex co)  //定义运算符/的重载函数
    {
     Complex temp;
     double t;
     t = 1/(co.real*co.real+co.imag*co.imag);
     temp.real = (real*co.real+imag*co.imag)*t;
     temp.imag = (co.real*imag-real*co.imag)*t;
     return temp;
    } 
    void Complex::print() 
    {
     cout<<real;
     cout<<"+"<<imag<<'i'<<endl; 
    } 
    int main()
    {
     Complex A1(2.3,4.6),A2(3.6,2.8),A3,A4,A5,A6;
     A3 = A1+A2; //A3 = A1.operaotr+(A2) 
     A4 = A1-A2; //A3 = A1.operaotr-(A2)
     A5 = A1*A2; //A3 = A1.operaotr*(A2)
     A6 = A1/A2; //A3 = A1.operaotr/(A2)
     A1.print();
     A2.print();
     A3.print();
     A4.print();
     A5.print();
     A6.print();
     return 0; 
    } 
    /*
     一般而言,如果在类X中采用成员函数重载双目运算符@,成员运算符函数operator@所需要的
     一个操作数由对象aa通过this指针隐含地传递,它的另一个操作数bb在参数表中显示,则以下
     两种函数调用方法是等价的。
      
          aa@bb;                 //隐式调用 
          aa.operator@(bb);      //显示调用 
    */ 

    3. (成员运算符重载函数)单目运算符重载

    对单目运算符而言,成员运算符重载函数的参数表中没有参数,此时当前对象作为运算符的一个操作数。 

    //例5.6 用成员函数重载单目运算符"++"

    #include<iostream>
    using namespace std;
    class Coord{
     public:
      Coord(int i=0,int j=0)
      {
       x = i;
       y = j;
      }
      Coord operator++();        //声明成员运算符++重载函数
      //void operator++();
      void print();
     private:
      int x,y;
    }; 
    Coord Coord::operator++()   //定义成员运算符++重载函数
    {
     ++x;
     ++y;
     return *this;              //返回当前对象的值 
    }
    /*
    void Coord::operator++()
    {
     ++x;
     ++y;
    }
    */
    void Coord::print()
    {
     cout<<"x="<<x<<","<<"y="<<y<<endl; 
    }
    int main()
    {
     Coord c(10,20);
     c.print();
     
     ++c;            //隐式调用 
     c.print();
     
     c.operator++(); //显示调用 
     c.print();
     
     return 0;
    }
    /*
      本例中主函数中调用成员运算符重载函数operator的两种方式是等价的。
        即  ++c ========== c.operator++()
        
      其格式为:     @aa;             //隐式调用
                    aa.operator();   //显示调用
                    
      从本例中还可以看出,当用成员函数重载单目运算时,没有参数被显示地传递给成员运算符
      。参数是通过this指针隐含地传递给函数              
                      
    */
  • 相关阅读:
    excel数据导入mySql数据库
    springboot-devtools实现项目的自动重启
    Java中List, Integer[], int[]的相互转换
    Postman代码测试工具如何用?
    git提交代码时,Unstaged changes如何过滤.class .log等文件
    Json字符串转map集合
    实现hibernate 的validator校验
    [转载]OpenSSL中文手册之命令行详解(未完待续)
    Openssl ASN.1 说明一 分享
    [转载]Parsing X.509 Certificates with OpenSSL and C
  • 原文地址:https://www.cnblogs.com/XYQ-208910/p/4912540.html
Copyright © 2011-2022 走看看