zoukankan      html  css  js  c++  java
  • C++之运算符重载(2)

      上一节主要讲解了C++里运算符重载函数,在看了单目运算符(++)重载的示例后,也许有些朋友会问这样的问题。++自增运算符在C或C++中既可以放在操作数之前,也可以放在操作数之后,但是前置和后置的作用又是完全不同的(q前置运算符:先加1,再赋值;后置运算符:先赋值,再加1)。那么要怎么重载它们,才可以有效的区分开来呢?今天我就来说说C++中是怎么处理前置运算符和后置运算符的重载的。以及介绍一下插入运算符(>>)和提取运算符(<<)的重载。

      1.在C++里编译器是根据运算符重载函数参数表里是否插入关键字int来区分前置还是后置运算。比如:

    #include "stdafx.h"
    #include <iostream>
    
    class TDPoint//三维坐标
    {
    private:
    int x;
    int y;
    int z;
    public:
            TDPoint(int x=0,int y=0,int z=0)
            {
    this->x=x;
    this->y=y;
    this->z=z;
            }
            TDPoint operator++();//成员函数重载前置运算符++
            TDPoint operator++(int);//成员函数重载后置运算符++
            friend TDPoint operator++(TDPoint& point);//友元函数重载前置运算符++
            friend TDPoint operator++(TDPoint& point,int);//友元函数重载后置运算符++
    void showPoint();
    };
    
    TDPoint TDPoint::operator++()
    {
    ++this->x;
    ++this->y;
    ++this->z;
    return*this;//返回自增后的对象
    }
    
    TDPoint TDPoint::operator++(int)
    {
        TDPoint point(*this);
    this->x++;
    this->y++;
    this->z++;
    return point;//返回自增前的对象
    }
    
    TDPoint operator++(TDPoint& point)
    {
    ++point.x;
    ++point.y;
    ++point.z;
    return point;//返回自增后的对象
    }
    
    TDPoint operator++(TDPoint& point,int)
    {
        TDPoint point1(point);
        point.x++;
        point.y++;
        point.z++;
    return point1;//返回自增前的对象
    }
    
    void TDPoint::showPoint()
    {
        std::cout<<"("<<x<<","<<y<<","<<z<<")"<<std::endl;
    }
    
    int main()
    {
        TDPoint point(1,1,1);
        point.operator++();//或++point
        point.showPoint();//前置++运算结果
    
        point=point.operator++(0);//或point=point++
        point.showPoint();//后置++运算结果
    
    operator++(point);//或++point;
        point.showPoint();//前置++运算结果
    
        point=operator++(point,0);//或point=point++;
        point.showPoint();//后置++运算结果
    
    return0;
    }

    结果:

    从示例代码里可以清楚的看出,后置运算符重载函数比前置运算符重载函数多了一个int类型的参数,这个参数只是为了区别前置和后置运算符,此外没有任何作用。所以在调用后置运算符重载函数时,int类型的实参可以取任意值。

      2.在C++中,操作符"<<"和">>"被定义为左位移运算符和右位移运算符。由于在iostream头文件中对它们进行了重载,使得它们可以用基本数据的输出和输入。

    #include "stdafx.h"
    #include <iostream>
    
    int main()
    {
        int a=10;
        std::cout<<"a="<<a<<std::endl;//运算符"<<"重载后用于输出
        a=a>>2;//右移运算符
        std::cout<<"右移2位:a="<<a<<std::endl;
    
        std::cout<<"请输入一个整数a:";
        std::cin>>a;//运算符">>"重载后用于输入
        a=a<<2;//左移运算符
        std::cout<<"左移2位:a="<<a<<std::endl;
    
        return0;
    }

    结果:

    插入运算符"<<"是双目运算符,左操作数为输出流类ostream的对象,右操作数为系统预定义的基本类型数据。头文件iostrem对其重载的函数原型为ostream& operator<<(ostream& ,类型名);类型名就是指基本类型数据。但如果要输出用户自定义的类型数据的话,就需要重载操作符"<<",因为该操作符的左操作数一定为ostream类的对象,所以插入运算符"<<"只能是类的友元函数或普通函数,不能是其他类的成员函数。一般定义格式:

      ostream& operator<<(ostream& ,自定义类名&);

    提取运算符">>"也是如此,左操作数为istream类的对象,右操作数为基本类型数据。头文件iostrem对其重载的函数原型为istream& operator>>(istream& ,类型名);提取运算符也不能作为其他类的成员函数,可以是友元函数或普通函数。它的一般定义格式为:

      istream& operator>>(istream& ,自定义类名&);

    我还是用上一节用的Complex类(复数类)来举例:

    #include "stdafx.h"
    #include <iostream>
    
    class Complex //复数类
    {
        private://私有
    double real;//实数
    double imag;//虚数
    public:
            Complex(double real=0,double imag=0)
            {
                this->real=real;
                this->imag=imag;
            }
            friend std::ostream&operator<<(std::ostream& o,Complex& com);//友元函数重载提取运算符"<<"
            friend std::istream&operator>>(std::istream& i,Complex& com);//友元函数重载插入运算符">>"
    };
    
    std::ostream&operator<<(std::ostream& o,Complex& com)
    {
        std::cout<<"输入的复数:";
        o<<com.real;
        if(com.imag>0)
            o<<"+";
        if(com.imag!=0)
            o<<com.imag<<"i"<<std::endl;
        return o;
    }
    
    std::istream&operator>>(std::istream& i,Complex& com)
    {
        std::cout<<"请输入一个复数:"<<std::endl;
        std::cout<<"real(实数):";
        i>>com.real;
        std::cout<<"imag(虚数):";
        i>>com.imag;
        return i;
    }
    
    int main()
    {
    
        Complex com;
        std::cin>>com;
        std::cout<<com;
    
        return0;
    }

    结果:

  • 相关阅读:
    html_dom类读取
    PHPExcel读取Excel文件的实现代码
    PHP中的mb_convert_encoding与iconv函数介绍
    BZOJ 3160 万径人踪灭 解题报告
    BZOJ 4036 [HAOI2015] Set 解题报告
    BZOJ 3288 Mato矩阵 解题报告
    BZOJ 3173 [Tjoi2013] 最长上升子序列 解题报告
    BZOJ 4123 [Baltic2015] Hacker 解题报告
    BZOJ 4127 Abs 解题报告
    BZOJ 4145 [AMPPZ2014] The Prices 解题报告
  • 原文地址:https://www.cnblogs.com/wuchanming/p/3879532.html
Copyright © 2011-2022 走看看