zoukankan      html  css  js  c++  java
  • C++学习28 重载>>和<<(输入输出运算符)

    在C++中,系统已经对左移运算符“<<”和右移运算符“>>”分别进行了重载,使其能够用于输入输出,但是输入输出的处理对象只能是系统内建的数据类型。系统重载这两个运算符是以系统类成员函数的形式进行的,因此cout<< var语句可以理解为:

    cout.operator<<( var )

    如果我们自己定义了一种新的数据类型,需要用输入输出运算符去处理,那么就要重载。本节以前面的 complex 类为例说明输入输出运算符的重载。

    重载输入运算符>>

    下面我们用全局函数的形式重载输入运算符,使它能够读入两个 double 类型的数据,并转换为一个复数,保存到复数对象中:

    istream & operator>>(istream & in, complex & A){
        in >> A.real >> A.imag;
        return in;
    }

    istream 是输入流,cin 就是 istream 类的对象,后续会讲解。因为重载运算符函数需要用到 complex 类的 private 成员变量,为了方便,我们将这个函数声明为 complex 类的友元函数。声明形式如下:

    friend istream & operator>>(istream & in , complex & a);

    该函数可以按照如下方式使用:

    complex c;
    cin>> c;

    当输入1.45 2.34↙后,这两个小数就分别成为 complex 对象 c 的实部和虚部了。cin>> c;这一语句其实可以理解为:

    operator<<(cin , c);

    在重载输入运算符时,采用引用的方式进行参数传递:输入的参数里面包含一个 istream 类的引用,返回值仍然为该引用。这样做的一个明显好处就是可以采用链式输入(也就是连续输入),如下所示:

    complex c1, c2, c3;
    cin>> c1 >> c2 >> c3;

    重载输出运算符<<

    同样的,我们也可以模仿上面的方式对输出运算符进行重载,让它能够输出复数。函数在类内部的声明如下:

    rriend ostream &(ostream & out, complex & A);

    全局函数的实现如下:

    ostream & operator<<(ostream & out, complex & A){
        out << A.real <<" + "<< A.imag <<" i ";
        return out;
    }

    与 istream 相反,ostream 表示输出流,cout 就是 ostream 类的对象。为了能够直接访问 complex 类的私有成员变量,同样需要将这个函数声明为 complex 类的友元函数。由于采用了引用的方式进行参数传递,该输出运算符重载函数可以实现链式输出。

    结合输入输出运算符的重载,重新实现 complex 类:

    #include <iostream>
    using namespace std;
    class complex{
    private:
        double real;  //复数的实部
        double imag;  //复数的虚部
    public:
        complex(): real(0.0), imag(0.0){ };
        complex(double a, double b): real(a), imag(b){ };
        friend complex operator+(const complex & A, const complex & B);
        friend complex operator-(const complex & A, const complex & B);
        friend complex operator*(const complex & A, const complex & B);
        friend complex operator/(const complex & A, const complex & B);
        friend istream & operator>>(istream & in, complex & A);
        friend ostream & operator<<(ostream & out, complex & A);
    };
    //重载加法运算符
    complex operator+(const complex & A, const complex &B){
        complex C;
        C.real = A.real + B.real;
        C.imag = A.imag + B.imag;
        return C;
    }
    //重载减法运算符
    complex operator-(const complex & A, const complex &B){
        complex C;
        C.real = A.real - B.real;
        C.imag = A.imag - B.imag;
        return C;
    }
    //重载乘法运算符
    complex operator*(const complex & A, const complex &B){
        complex C;
        C.real = A.real * B.real - A.imag * B.imag;
        C.imag = A.imag * B.real + A.real * B.imag;
        return C;
    }
    //重载除法运算符
    complex operator/(const complex & A, const complex & B){
        complex C;
        double square = A.real * A.real + A.imag * A.imag;
        C.real = (A.real * B.real + A.imag * B.imag)/square;
        C.imag = (A.imag * B.real - A.real * B.imag)/square;
        return C;
    }
    //重载输入运算符
    istream & operator>>(istream & in, complex & A){
        in >> A.real >> A.imag;
        return in;
    }
    //重载输出运算符
    ostream & operator<<(ostream & out, complex & A){
        out << A.real <<" + "<< A.imag <<" i ";;
        return out;
    }
    int main(){
        complex c1, c2, c3;
        cin>>c1>>c2;
      
        c3 = c1 + c2;
        cout<<"c1 + c2 = "<<c3<<endl;
        c3 = c1 - c2;
        cout<<"c1 - c2 = "<<c3<<endl;
        c3 = c1 * c2;
        cout<<"c1 * c2 = "<<c3<<endl;
        c3 = c1 / c2;
        cout<<"c1 / c2 = "<<c3<<endl;
        return 0;
    }

    在本例中,我们均采用全局函数的形式进行运算符重载,这样在输入输出时方便了不少。

  • 相关阅读:
    C#通过“委托和事件”的方式实现进程监控并与“普通方式”对比
    SharePoint自动化系列——通过PowerShell创建SharePoint Web
    SharePoint自动化系列——通过PowerShell创建SharePoint Site Collection
    IE11不支持Selenium 2.0的解决方法
    C#中WebBrowser控件的使用
    SharePoint自动化系列——Solution auto-redeploy using Selenium(C#)
    多进程监控自动关机工具升级远程关闭多台server——C# works with PowerShell
    通过Windows PowerShell远程管理计算机(精简版)
    .NET项目web自动化测试实战——Selenium 2.0
    HP LoadRunner 12.02 Tutorial T7177-88037教程独家中文版
  • 原文地址:https://www.cnblogs.com/Caden-liu8888/p/5827579.html
Copyright © 2011-2022 走看看