第六章流类和输入.ppt
文本预览下载声明
第六章 流类与输入 / 输出;
格式,状态信息 缓存类 缓存
ios中一根指针
ios类:I/O 基类:提供格式、错误检测和状态信息
ios中有一streambuf类指针。streambuf 类
再指向缓存。
istream类:创建输入流(重载了见下)提供输入操作。
ostream类:创建输出流(重载了见下)提供输出操作。
iostream类:创建输入/出流,提供输入/出操作。
class istream : public virtual ios { … }
class ostream : public virtual ios { … }
class iostream : public istream , public ostream { … }; 预定义流 物理设备
;ξ6.3 流库中定义的 提取( )和 插入( ) 操作
1. ()提取, 为istream 类,重载各种基本类型
重载函数格式:
istream istream::operator ( 参数类型 )
表明:下列语句可以直接使用,是由于在istream中重载了
istream istream::operator ( int )
int a ;
cin a ;;()插入为ostream 类重载了各种基本类型
ostream ostream::operator ( 参数类型 )
, 输入/出,仅适用于已定义的参数类型
若自定义的类对象,(nameclass)就不可直接施
于 cin nameclass 或cout nameclass。
必须对类重载:operator (…) 或 operator
(…) 。且只能 重载为友元函数。
;例(1)类矩形Rectangle,以友元函数重载 ,作类对象输出.
#include iostream.h
class Rectangle
{ private :
int width ;
int height ;
public :
Rectangle ( int w , int h )
{ width = w ;
height = h ;
}
friend ostream operator ( ostream stream , Rectangle ob ) ;
friend istream operator ( istream stream, Rectangle ob );
} ;;ostream operator ( ostream stream , Rectangle ob )
{ stream “宽:” ob.width endl ;
stream “高:” ob.height endl ;
return stream ;
}
istream operator ( istream stream, Rectangle
显示全部