一、基本概念:
C++的流插入运算符“<<”;流提取运算符“>>”是在编译系统的类库中提供的的。
C++在编译系统都在其类库中提供输入流类istream和输出流类ostream。
cin和cout分别是istream和ostream类的对象。
对“<<”和“>>”的重载如下:
istream operator<<(istream &,自定义类&);
ostream operator<<(ostream &,自定义类&);
只能将重载“<<”和“>>”的函数作为友元函数而不能将它们定义为成员函数。
二、重载流插入运算符"<<"
#include <iostream>
using namespace std;
class Complex
{
public:
Complex(){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
Complex operator+(Complex &c2);
friend ostream& operator <<(ostream&,Complex&);
private:
double real;
double imag;
};
Complex Complex::operator+(Complex &c2)
{
retur