重载流插入运算符和流提取运算符
1、流插入/提取运算符
cout <<
:流插入运算符
cin >>
:流提取运算符
重载使之用于自定义的类对象,只能重载为类的友元
(1)重载流插入运算符
重载流插入运算符的一般格式:
ostream &operator << ( ostream &output , 类名&对象名 ) {
……
return output
}
【示例】流插入运算符重载为成员函数
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
class myComplex
{
private:
double real,imag;
public:
myComplex():real(0),imag(0){}
myComplex(double r, double i):real(r),imag(i){}
ostream & operator<<(ostream & os); //成员函数,插入
friend istream & operator>>(istream & is,myComplex & c); //友元,提取
};
ostream & myComplex::operator<<(ostream & os) //类的成员函数
{
if(this->imag>=0)
os<<this->real<<"+"<<this->imag<<"i"; ///a+bi的的形式输出
e1se
os<<this->real<<"+"<<(-this->imag)<<"i";
return os;
}
istream & operator>>(istream & is,myComplex & c)
{
string s;
is>>s; //将a+bi作为字符串读入,a+bi中间不能有空格
int pos = s.find("+",0); //查找虚部
if(pos==-1)
pos=s.find("-",1); //虚部为负数时
string sReal=s.substr(0,pos); //分离出代表实部的字符中
c.real=atof(sReal,c _str()); //atof()能将参数内容转换成浮点数
sReal=s.substr(pos,s.length()-pos-1); //分离出代表虚部的宇符串
c.imag=atof(sReal.c str());
return is;
}
int main()
{
myComplex c,c1;
int n;
cout<<"请输入两个复数([-]a+-bi)和一个整数,以空格分隔"<<endl;
cin>>c>>c1>>n;
cout<<(ck<cout<<",")<<","<<n<<end1; //注意调用的形式
return 0;
}
(2)重载流提取运算符
重载流提取运算符的一般格式
istream &operator >> ( istream & input , 类名&对象名 ) {
……
return input
}
【示例】
#include<string>
#include<cstdlib>
using namespace std;
class myComplex
{
private:
double real,imag ;
public:
myComplex(): real(0),imag(0)()
myComplex(double r,double i):real(r),imag(i){}
friend ostream & operator<<(ostream & os,const myComplex & c); //友元,插入
friend istream & operator>>(istream & is,myComplex & c); //友元,提取
}
ostream & operator<<(ostream & os,const myComplex & c)
{
if(c.imag>=0)
os<<c.real<<"+"<<c.imag<<"i"; //以a+bi的形式输出
e1se
os<<c.real<<"+"<<(-c.imag)<<"i";
return os;
}
istream & operator>>(istream & is,myComplex & c)
{
string s;
is>>s; //将a+bi作为宁符串读入,a+bi中间不能有空格
int pos = s.find("+",0); //查找虚部
if(pos==-1)
pos=s.find("-",1); //虚部为负数时
string sReal=s.substr(0,pos); //分离出代表父部的字符串
c.real=atof(sReal,c_str()); //atof() 能将参数内转换成浮点数
sReal=s.substr(pos,s.length()-pos-1); //分离出代表虚部的字符串
c.imag=atof(sReal.c str());
return is;
}
int main()
{
myComplex c,c1;
int n;
cout<<"请输入两个复数([-]a+-bi)和一个整数,以空格分隔"<<end1;
cin>>c>>c1>>n;
cout<<c<<","<<n<<","<<cl<<endl:
return 0;
}
2、例题
例题1:[单选题]如果类A要重载流插入运算符<<,则重载函数参数表的形式一般定义为( )
A. (const A&)
B.(ostream&)
C.(const A&, ostream&)
D.(ostream&, const A&)
【正确答案】:D
【答案解析】:重载插入运算符一般格式为:ostream &operator<<(ostream & output,类名&对象名){}
例题2:[完成程序题]下列程序有三个地方需要补充完整,使程序显示 (6.8)
#include<iostream>
using std::ostream;
using std::cout;
using std::endl ;
//表示二维向量的类
class MyVector
{
double x;//X坐标值
double y;//Y坐标值
public:
MyVector (double i=0.0 , double j=0.0);//构造函数
MyVector operator+(MyVector j);//重载运算符+
friend MyVector operator-( MyVector i, MyVector j); //重载运算符-
friend ostream& operator<<(ostream& os, MyVector v); //重载运算符<<
};
____(1)_____(double i , double j): x(i), y (j){}
MyVector MyVector::operator+( MyVector j)
{
return MyVector (x+j.x, y+j.y) ;
}
MyVector operator-( MyVector i, MyVector j)
{
return MyVector ( _________(2)____________ ) ;
}
ostream& operator<<( ostream& os, MyVector v)
{
os << '(' << v.x <<','<< v.y << ')';//输出向量v的坐标
return os:
}
int main()
{
MyVector v1(2,3),v2 (4,5), v3;
v3=_____(3)_______;
cout<<v3<<endl ;
return 0;
}
【正确答案】:
(1) MyVector
(2) i.x - j.x , i.y - j.y
(3) y1+v2
【答案解析】:运算符重载可重载为成员函数,也可重载为友元函数。