C++重载输入与输出操作符

在 C++ 中,系统已经对左移操作符“<<”和右移操作符“>>”分别进行了重载,使其能够用作输入输出操作符,但是输入输出的处理对象只限于系统内建的数据类型。系统重载这两个操作符是以系统类成员函数的形式进行的,因此cout<< var; 语句可以理解为:
cout.operator<<(var);
如果我们自己定义了一种新的数据类型,需要用输入输出操作符去处理,则需要重载 >> 和 << 这两个操作符。

接下来,我就以前面章节定义好的 complex 类为例,说明重载输入输出操作符的方法。要知道,系统重载的 << 和 >> 运算符不适用于 complex 类对象, 如果想直接输入输出复数的话,就必须对这两个操作符进行重载。

我们可以重载输入操作符,使之读入两个 double 类型数据,并且将之转换为一个复数,并存入到一个复数类对象中。我们采用顶层函数的形式来实现输入操作符的重载。
istream & operator>>(istream & in, complex & A)
{
    in >> A.real >> A.imag;
    return in;
}
istream 是指输入流,这个将会在后面讲到。因为重载操作符函数需要用到 complex 类的私有成员变量,为了方便,我们将这个函数声明为 complex 类的友元函数。其声明形式如下:
friend istream & operator>>(istream & in , complex & a);
该函数可以按照如下方式使用:
complex c;
cin>> c;
有了这两个语句后,我们输入(表示用户按下 Enter 回车键)

1.45 2.34↙

这两个数据就分别成为了复数类对象 c 的实部和虚部。cin>> c;语句可以理解为:
operator<<(cin , c);
在重载输入操作符时,传递参数采用的是引用的方式,输入的参数里面包含一个 istream 流的引用,返回值仍然为该引用,因此我们可以连续输入多个 complex 类对象:
complex c1, c2;
cin>> c1 >> c2;
同样的,我们也可以将输出操作符进行重载,使之能够输出复数。函数在类内部的声明如下:
friend ostream &(ostream & out, complex & A);
顶层函数的实现如下:
ostream & operator<<(ostream & out, complex & A)
{
    out << A.real <<" + "<< A.imag <<" i ";
    return out;
}
与 istream 一样,ostream 用于表示输出流,同样为了能够直接访问 complex 类的私有成员变量,我们将其在类内部声明为 complex 类的友元函数。该输出操作符重载函数可以连续输出多个 complex 类对象。

结合输入输出操作符的重载,如下给出了 complex 类的完整实现:
#include <iostream>
using namespace std;

class complex
{
public:
    complex();
    complex(double a);
    complex(double a, double 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);
    void display()const;
private:
    double real;   //复数的实部
    double imag;   //复数的虚部
};

complex::complex()
{
    real = 0.0;
    imag = 0.0;
}

complex::complex(double a)
{
    real = a;
    imag = 0.0;
}

complex::complex(double a, double b)
{
    real = a;
    imag = b;
}

//打印复数
void complex::display()const
{
    cout<<real<<" + "<<imag<<" i ";
}

//重载加法操作符
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(4.3, -5.8);
    complex c2(8.4, 6.7);
    complex c3;
   
    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;
}
程序中均采用顶层函数的形式重载操作符,为了能够方便访问类中的私有成员变量,我们将这些操作符重载函数声明为 complex 类的友元函数。

我们直接来看主函数。在主函数的一开始,我们定义了三个 complex 类的对象,并且 c1 和 c2 都用构造函数对复数进行赋值。在这一步我们其实也可以用cin>> c1 >> c2;来替代,因为我们已经重载了输入操作符。在之前输出复数都必须通过类中的 display 函数,但是重载了输出操作符之后,就不需要这么麻烦了,直接输出就可以了,写法上简单不少。