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

    转载:http://c.biancheng.net/view/2311.html

    C++中,标准库本身已经对左移运算符<<和右移运算符>>分别进行了重载,使其能够用于不同数据的输入输出,但是输入输出的对象只能是 C++ 内置的数据类型(例如 bool、int、double 等)和标准库所包含的类类型(例如 string、complex、ofstream、ifstream 等)。

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

    其实 C++ 标准库已经提供了 complex 类,能够很好地支持复数运算,但是这里我们又自己定义了一个 complex 类,这样做仅仅是为了教学演示。

    本节要达到的目标是让复数的输入输出和 int、float 等基本类型一样简单。假设 num1、num2 是复数,那么输出形式就是:

    cout<<num1<<num2<<endl;

    输入形式就是:

    cin>>num1>>num2;

    cout 是 ostream 类的对象,cin 是 istream 类的对象,要想达到这个目标,就必须以全局函数(友元函数)的形式重载<<>>,否则就要修改标准库中的类,这显然不是我们所期望的。

    重载输入运算符>>

    下面我们以全局函数的形式重载>>,使它能够读入两个 double 类型的数据,并分别赋值给复数的实部和虚部:

    1. istream & operator>>(istream &in, complex &A){
    2. in >> A.m_real >> A.m_imag;
    3. return in;
    4. }
    istream & operator>>(istream &in, complex &A){
        in >> A.m_real >> A.m_imag;
        return in;
    }

    istream 表示输入流,cin 是 istream 类的对象,只不过这个对象是在标准库中定义的。之所以返回 istream 类对象的引用,是为了能够连续读取复数,让代码书写更加漂亮,例如:

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

    如果不返回引用,那就只能一个一个地读取了:

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

    另外,运算符重载函数中用到了 complex 类的 private 成员变量,必须在 complex 类中将该函数声明为友元函数:

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

    >>运算符可以按照下面的方式使用:

    complex c;
    cin>>c;

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

    operator<<(cin , c);

    重载输出运算符<<

    同样地,我们也可以模仿上面的形式对输出运算符>>进行重载,让它能够输出复数,请看下面的代码:

    1. ostream & operator<<(ostream &out, complex &A){
    2. out << A.m_real <<" + "<< A.m_imag <<" i ";
    3. return out;
    4. }
    ostream & operator<<(ostream &out, complex &A){
        out << A.m_real <<" + "<< A.m_imag <<" i ";
        return out;
    }

    ostream 表示输出流,cout 是 ostream 类的对象。由于采用了引用的方式进行参数传递,并且也返回了对象的引用,所以重载后的运算符可以实现连续输出。

    为了能够直接访问 complex 类的 private 成员变量,同样需要将该函数声明为 complex 类的友元函数:

    friend ostream & operator<<(ostream &out, complex &A);

    综合演示

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

    1. #include <iostream>
    2. using namespace std;
    3.  
    4. class complex{
    5. public:
    6. complex(double real = 0.0, double imag = 0.0): m_real(real), m_imag(imag){ };
    7. public:
    8. friend complex operator+(const complex & A, const complex & B);
    9. friend complex operator-(const complex & A, const complex & B);
    10. friend complex operator*(const complex & A, const complex & B);
    11. friend complex operator/(const complex & A, const complex & B);
    12. friend istream & operator>>(istream & in, complex & A);
    13. friend ostream & operator<<(ostream & out, complex & A);
    14. private:
    15. double m_real; //实部
    16. double m_imag; //虚部
    17. };
    18.  
    19. //重载加法运算符
    20. complex operator+(const complex & A, const complex &B){
    21. complex C;
    22. C.m_real = A.m_real + B.m_real;
    23. C.m_imag = A.m_imag + B.m_imag;
    24. return C;
    25. }
    26.  
    27. //重载减法运算符
    28. complex operator-(const complex & A, const complex &B){
    29. complex C;
    30. C.m_real = A.m_real - B.m_real;
    31. C.m_imag = A.m_imag - B.m_imag;
    32. return C;
    33. }
    34.  
    35. //重载乘法运算符
    36. complex operator*(const complex & A, const complex &B){
    37. complex C;
    38. C.m_real = A.m_real * B.m_real - A.m_imag * B.m_imag;
    39. C.m_imag = A.m_imag * B.m_real + A.m_real * B.m_imag;
    40. return C;
    41. }
    42.  
    43. //重载除法运算符
    44. complex operator/(const complex & A, const complex & B){
    45. complex C;
    46. double square = A.m_real * A.m_real + A.m_imag * A.m_imag;
    47. C.m_real = (A.m_real * B.m_real + A.m_imag * B.m_imag)/square;
    48. C.m_imag = (A.m_imag * B.m_real - A.m_real * B.m_imag)/square;
    49. return C;
    50. }
    51.  
    52. //重载输入运算符
    53. istream & operator>>(istream & in, complex & A){
    54. in >> A.m_real >> A.m_imag;
    55. return in;
    56. }
    57.  
    58. //重载输出运算符
    59. ostream & operator<<(ostream & out, complex & A){
    60. out << A.m_real <<" + "<< A.m_imag <<" i ";;
    61. return out;
    62. }
    63.  
    64. int main(){
    65. complex c1, c2, c3;
    66. cin>>c1>>c2;
    67.  
    68. c3 = c1 + c2;
    69. cout<<"c1 + c2 = "<<c3<<endl;
    70.  
    71. c3 = c1 - c2;
    72. cout<<"c1 - c2 = "<<c3<<endl;
    73.  
    74. c3 = c1 * c2;
    75. cout<<"c1 * c2 = "<<c3<<endl;
    76.  
    77. c3 = c1 / c2;
    78. cout<<"c1 / c2 = "<<c3<<endl;
    79.  
    80. return 0;
    81. }
    #include <iostream>
    using namespace std;
    
    class complex{
    public:
        complex(double real = 0.0, double imag = 0.0): m_real(real), m_imag(imag){ };
    public:
        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);
    private:
        double m_real;  //实部
        double m_imag;  //虚部
    };
    
    //重载加法运算符
    complex operator+(const complex & A, const complex &B){
        complex C;
        C.m_real = A.m_real + B.m_real;
        C.m_imag = A.m_imag + B.m_imag;
        return C;
    }
    
    //重载减法运算符
    complex operator-(const complex & A, const complex &B){
        complex C;
        C.m_real = A.m_real - B.m_real;
        C.m_imag = A.m_imag - B.m_imag;
        return C;
    }
    
    //重载乘法运算符
    complex operator*(const complex & A, const complex &B){
        complex C;
        C.m_real = A.m_real * B.m_real - A.m_imag * B.m_imag;
        C.m_imag = A.m_imag * B.m_real + A.m_real * B.m_imag;
        return C;
    }
    
    //重载除法运算符
    complex operator/(const complex & A, const complex & B){
        complex C;
        double square = A.m_real * A.m_real + A.m_imag * A.m_imag;
        C.m_real = (A.m_real * B.m_real + A.m_imag * B.m_imag)/square;
        C.m_imag = (A.m_imag * B.m_real - A.m_real * B.m_imag)/square;
        return C;
    }
    
    //重载输入运算符
    istream & operator>>(istream & in, complex & A){
        in >> A.m_real >> A.m_imag;
        return in;
    }
    
    //重载输出运算符
    ostream & operator<<(ostream & out, complex & A){
        out << A.m_real <<" + "<< A.m_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;
    }

    运行结果:
    2.4 3.6↙
    4.8 1.7↙
    c1 + c2 = 7.2 + 5.3 i
    c1 - c2 = -2.4 + 1.9 i
    c1 * c2 = 5.4 + 21.36 i
    c1 / c2 = 0.942308 + 0.705128 i

  • 相关阅读:
    广度优先搜索(一)
    快速幂
    office 2013
    最著名的十大公式
    二分查找的上下界
    双关键字快速排序
    字符串操作
    分治算法练习(二)
    P3119 [USACO15JAN]草鉴定[SCC缩点+SPFA]
    P3225 [HNOI2012]矿场搭建[割点]
  • 原文地址:https://www.cnblogs.com/MCSFX/p/13524605.html
Copyright © 2011-2022 走看看