技术交流QQ群:1027579432,欢迎你的加入!

1.C++中重载输入cin和输出cout运算符

  • 在C++中,标准库本身已经对左移运算符<<和右移运算符>>分别进行了重载,使其能够用于不同数据的输入输出,但是输入输出的对象只能是 C++内置的数据类型(例如 bool、int、double 等)和标准库所包含的类类型(例如 string、complex、ofstream、ifstream 等)。如果自己定义了一种新的数据类型,需要用输入输出运算符去处理,那么就必须对它们进行重载。本节以complex类为例来演示输入输出运算符的重载。
  • 本节要达到的目标是让复数的输入输出和int、float等基本类型一样简单。假设num1、num2是复数:
        cout << num1 << num2 << endl;  // 输出形式
        cin >> num1 >> num2 << endl;   // 输入形式
    
  • cout是ostream类的对象,cin是istream类的对象,要想达到这个目标,就必须以全局函数(友元函数)的形式重载<<和>>,否则就要修改标准库中的类,这显然不是我们所期望的

2.重载输入运算符>>

  • 以全局函数的形式重载>>,使它能够读入两个double类型的数据,并分别赋值给复数的实部和虚部:
        istream & operator>> (istream &in, complex &A);{
            in >> A.real >> A.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);
    

3.重载输出运算符<<

  • 同样地,也可以模仿上面的形式对输出运算符>>进行重载,让它能够输出复数,请看下面的代码:
        ostream & operator<< (ostream &out, complex &A){
            out << A.real << "+" << A.imag << "i" << endl;
            return out;
        }
    
  • ostream表示输出流,cout是ostream类的对象。由于采用了引用的方式进行参数传递,并且也返回了对象的引用,所以重载后的运算符可以实现连续输出。为了能够直接访问complex类的private成员变量,同样需要将该函数声明为complex类的友元函数:
        friend ostream & operator<< (ostream &out, complex &A);
    

4.综合实例如下:

```
    #include "iostream"

    using namespace std;

    class complex
    {
    public:
        complex(double a = 0.0, double b = 0.0) : real(a), img(b){};

    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 real;
        double img;
    };

    // 重载加法运算符
    complex operator+(const complex &A, const complex &B)
    {
        complex C;
        C.real = A.real + B.real;
        C.img = A.img + B.img;
        return C;
    }
    // 重载减法运算符
    complex operator-(const complex &A, const complex &B)
    {
        complex C;
        C.real = A.real - B.real;
        C.img = A.img - B.img;
        return C;
    }

    // 重载乘法运算符
    complex operator*(const complex &A, const complex &B)
    {
        complex C;
        C.real = A.real * B.real - A.img * B.img;
        C.img = A.img * B.real + A.real * B.img;
        return C;
    }

    // 重载除法运算符
    complex operator/(const complex &A, const complex &B)
    {
        complex C;
        double square = A.real * A.real + A.img * A.img;
        C.real = (A.real * B.real + A.img * B.img) / square;
        C.img = (A.img * B.real - A.real * B.img) / square;
        return C;
    }

    // 重载输入运算符
    istream &operator>>(istream &in, complex &A)
    {
        in >> A.real >> A.img;
        return in;
    }

    // 重载输出运算符
    ostream &operator<<(ostream &out, complex &A)
    {
        out << A.real << " + " << A.img << "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;
    }
```