面向对象程序设计与C++语言 第二版 教学课件 作者 杨庚 王汝传 叶晓国 第十一讲.ppt
文本预览下载声明
* * 1. 基类中的虚函数是由 virtual 定义的: virtual void print( ); 2. 在定义了虚函数之后, 其多态性在派生类中得 到延伸。 3. 在定义了虚函数之后, 只要定义一个基类的指 针,就可以调用子类的 函数. 输出与输入 9.1 C++流库结构 9.2 一般的输入/输出 9.3 输入/输出格式控制 第十章 输入/输出流库 §9.1 C++流库结构 什么是C++输出/输入流系统 指对流的操作集合,它将数据流向流对象,或从流对象流出数据。 什么是流 是有向的数据流动的抽象描述,是数据流动的渠道和方向,是程序与输入/输出设备的连接桥梁。 例1: cin name; //从流对象(键盘)读数据放入变量中 cout ?my name is ? name endl; //将数据写到流对象(屏幕)中 §9.1 C++流库结构 C++流库结构 类:ios 类:istream 类:ostream 类:ifstream 类:iostream 类:ofstream 类:fstream I/O流的派生结构: §9.2 一般的输入/输出 一般形式的输入:包含在iostream.h get ? 输入一个字符 getline ? 输入一行字符 ? 输入运算符 一般形式的输出:包含在iostream.h put ? 输出一个字符 write ? 输出 n 个字符 ? 输出运算符 一般形式的输出:包含在iostream.h cin ? 设备输入流对象(键盘) out ? 设备输出流对象(屏幕) 例1:输入/输出运算符 和 的应用 #include iostream.h void main( ) { int x=56, y=10; float a; char *str =Windows; cin a; cout x y endl; cout a endl; cout str endl; } 结果:123.45 56 10 123.45 Windows §9.2 一般的输入/输出 例2:输入/输出流的应用 #include iostream.h void main( ) { int x, y; char c; cout Enter two integers: cin x y; cout x (x==y? is: is not ) equal to y endl; c=cin.get( ); //从键盘输入一字符 cout.put(c); //从屏幕输出一字符 } 结果:Enter two integers: 7 5 7 is not equal to 5 A A §9.2 一般的输入/输出 例3:cin 和 get 的区别 #include iostream.h const int SIZE =80; void main( ) { char buffer1[SIZE], buffer2[SIZE]; cout Enter a sentence: endl; cin buffer1; cout endl The string read with cin was : buffer1 endl; cin.get(buffer2,SIZE); cout The string read with cin.get was: buffer2 endl; } 结果:Enter a sentence Test reading functions with cin and cin.get The string read win cin was: Test The string read cin.get was: reading functions with cin and cin.get §9.2 一般的输入/输出 例4:getline 和 write 的应用 #include iostream.
显示全部