在 C++ 中,左移运算符<
cout 是 ostream 类的对象。ostream 类和 cout 都是在头文件 中声明的。ostream 类将<
ostream & ostream::operator << (const char* s)
{
//输出s的代码
return * this;
}
为了使cout<<5;能够成立,ostream 类还需要将<
ostream & ostream::operator << (int n)
{
//输出n的代码
return *this;
}
重载函数的返回值类型为 ostream 的引用,并且函数返回 *this,就使得cout<
( cout.operator<
重载函数返回 *this,使得cout<
cin 是 istream 类的对象,是在头文件 中声明的。istream 类将>>重载为成员函数,因此 cin 才能和>>连用以输入数据。一般也将>>称为“流提取运算符”或者“输入运算符”。
例题:假定 c 是 Complex 复数类的对象,现在希望写cout<>c;就能从键盘接受 a+bi 形式的输入,并且使得 c.real = a, c.imag = b。
显然,要对<>进行重载,程序如下:
#include
#include
#include
using namespace std;
class Complex
{
double real,imag;
public:
Complex( double r=0, double i=0):real(r),imag(i){ };
friend ostream & operator<
friend istream & operator>>( istream & is,Complex & c);
};
ostream & operator<
{
os << c.real << "+" << c.imag << "i"; //以"a+bi"的形式输出
return os;
}
istream & operator>>( istream & is,Complex & c)
{
string s;
is >> s; //将"a+bi"作为字符串读入, "a+bi" 中间不能有空格
int pos = s.find("+",0);
string sTmp = s.substr(0,pos); //分离出代表实部的字符串
c.real = atof(sTmp.c_str());//atof库函数能将const char*指针指向的内容转换成 float
sTmp = s.substr(pos+1, s.length()-pos-2); //分离出代表虚部的字符串
c.imag = atof(sTmp.c_str());
return is;
}
int main()
{
Complex c;
int n;
cin >> c >> n;
cout << c << "," << n;
return 0;
}
程序的运行结果:
13.2+133i 87
13.2+133i,87
因为没有办法修改 ostream 类和 istream 类,所以只能将<>重载为全局函数的形式。由于这两个函数需要访问 Complex 类的私有成员,因此在 Complex 类定义中将它们声明为友元。
cout<
第 13 行,参数 os 只能是 ostream 的引用,而不能是 ostream 对象,因为 ostream 的复制构造函数是私有的,没有办法生成 ostream 参数对象。operator<< 函数的返回值类型设为 ostream &,并且返回 os,就能够实现<
用 cin 读入复数时,对应的输入必须是 a+bi 的格式,而且中间不能有空格,如输入 13.2+33.4i。第 21 行的is>>s;读入一个字符串。假定输入的格式没有错误,那么被读入 s 的就是 a+bi 格式的字符串。
读入后需要将字符串中的实部 a 和虚部 b 分离出来,分离的办法就是找出被+隔开的两个子串,然后将两个字符串转换成浮点数。第 24 行调用了标准库函数 atof 来将字符串转换为浮点数。该函数的原型是float atof(const char *),它在 头文件中声明。