1,基类中的虚函数是由
virtual 定义的,
virtual void print( );
2,在定义了虚函数之后,
其多态性在派生类中得到延伸。
3,在定义了虚函数之后,
只要定义一个基类的指针,就可以调用子类的函数,
输出与输入
10.1 C++流库结构
10.2 一般的输入 /输出
10.3 输入/输出格式控制第十章 输入 /输出流库
§ 10.1 C++流库结构什么是 C++输出 /输入流系统指对流的操作集合,它将数据流向流对象,或从流对象流出数据。
什么是流是有向的数据流动的抽象描述,是数据流动的渠道和方向,是程序与输入 /输出设备的连接桥梁。
例 1,cin >> name; //从流对象(键盘)读数据放入变量中
cout <<?my name is? << name << endl;
//将数据写到流对象(屏幕)中
§ 10.1 C++流库结构
C++流库结构类,ios
类,istream 类,ostream
类,ifstream 类,iostream 类,ofstream
类,fstream
I/O流的派生结构:
§ 10.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
§ 10.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
§ 10.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
§ 10.2 一般的输入 /输出例 4,getline 和 write 的应用
#include <iostream.h>
const int SIZE =80;
void main( ) {
char buffer[SIZE];
cout << "Enter a sentence," << endl;
cin.getline(buffer,SIZE);
cout << endl << "The string read was," << buffer << endl;
cout.write(buffer,8);
cout << endl;
cout.write("Hello world! ",9); }
结果,Enter a sentence
Test reading function with getline
The string read was,Test reading function with getline
The stri
Hello wor
§ 10.2 一般的输入 /输出注意点
1,cin输入多个数据时,键盘输入不要用逗号隔开,只需用空格或换行将数值隔开。 如:
cin >> x >> y;
输入为,10 20
不能输入为,10,20
2,类型的匹配问题:不同类型的变量一起输入时,系统除检查是否有空白,还要检查输入数据与变量的匹配问题。如:
cin >> i >> x //i为整型,x为实型若输入 56.79 32.85
结果为,i=56,x=0.79
而不是 i=56,x=32.85
3,输入字符串时,字符串中不能有空格,一旦遇到空格,就当着是本数据结束。如:
cin >> str;
当输入为 We are students!
结果为 str=We,而不是 str=We are student!
§ 10.2 一般的输入 /输出控制输入 /输出格式的目的
进行不同进制之间的转换,8进制,16进制等
精度的控制:小数点的位数等
输入 /输出的宽度控制。如:是输出 100,还是
100
格式之间的状态转换控制输入 /输出格式的方法
使用 ios类中的有关格式控制的成员函数即由 cin 或 cout 流对象来调用其成员函数如,cout.width(4); cout.precision(5);
使用操作函数控制格式不要前缀流对象,直接调用操作函数。可以直接嵌入到输入 /输出语句中。
cout << setw(10) << hex << n << endl;
§ 10.3 输入 /输出的格式控制例 5:
#include<iostream.h>
#include<iomanip.h>
void main( )
{ int n;
cout << " Enter a decimal number,";
cin >> n;
cout << n << " in hexadecimal is,"
<< hex << n << endl
<< dec << n << " in octal is,"
<< oct << n << endl
<< setbase(10) << n << " in decimal is," << n << endl;}
§ 10.3 输入 /输出的格式控制整数的不同进制转换结果,Enter a decimal number,20
20 in hexadecimal is,14
20 in octal is,24
20 in decimal is,20
例 6:
#include<iostream.h>
#include<iomanip.h>
#include<math.h>
void main( )
{ double x=sqrt(2.0);
for(int index=0; index<=7; index++)
{ //使用 ios的成员函数
cout.precision(index);
cout << x << endl; }
cout << endl;
for(int index=0; index<=9; index++)
{ cout << setprecision(index)
<< x << endl; //使用操作函数 } }
§ 10.3 输入 /输出的格式控制实数的精度控制 结果,1.4142141.4
1.41
1.414
1.4142
1.41421
1.414214
1.4142136
1.414214
1.4
1.41
1.414
1.4142
1.41421
1.414214
1.4142136
例 7:
#include<iostream.h>
#include<iomanip.h>
void main( )
{ int x=12345;
//缺省格式 与右端对齐
cout << "Default is on right:" << endl
<< setw(10) << x << endl << endl;
//调整到与左端对齐
cout.setf(ios::left,ios::adjustfield);
cout << setw(10) << x << endl << endl;
//再次调整到与右端对齐
cout.unsetf(ios::left);
cout << setw(10) << x << endl; }
§ 10.3 输入 /输出的格式控制打印宽度的控制结果:
Default is on right:
12345
12345
12345
例 8:
#include<iostream.h>
#include<iomanip.h>
void main( )
{ int x=12345;
//缺省格式 与右端对齐
cout << "Default is on right:" << endl
<< setw(10) << x << endl << endl;
//调整到与右端对齐
cout.setf(ios::right,ios::adjustfield);
cout.fill('* ');
cout << setw(10) << x << endl << endl;
//再次调整到与左端对齐
cout.setf(ios::left,ios::adjustfield);
cout << setw(10) << setfill(' % ') << x << endl; }
§ 10.3 输入 /输出的格式控制格式输出中空白的填充结果:
Default is on right:
12345
*****12345
12345%%%%%
例 9:
#include<iostream.h>
void main( )
{ double x=0.001234567,y=1.946e9;
//缺省格式输出
cout << "Displayed in default format:" << endl
<< x << '\t ' << y << endl << endl;
//调整到科学计算输出格式
cout.setf(ios:scientific,ios::floatfield);
cout << "Displayed in scientific format:" << endl
<< x << ' \t ' << y << endl;
//再次调整到固定输出格式
cout.setf(ios:fixed,ios::floatfield);
cout << "Displayed in fixed format:" << endl
<< x << ' \t ' << y << endl; }
§ 10.3 输入 /输出的格式控制科学计算等格式输出结果:
Displayed in default format:
0.001235 1.946e+09
Displayed in scientific format:
1.234567e-03 1.946e+09
Displayed in fixed format:
0.001235 1946000000
1,ios 流库的结构,istream 和 ostream
3,输入 /输出格式控制的方法:
使用 ios 的成员函数
使用操作函数
cin.getline(buffer,SIZE);
cout.width(10);
cout << setw(10) << x;
cout << setw(10) << setfill('* ') << x;
2,流对象 cin一般指键盘。流对象 cout一般指屏幕。
1,确定下列输出的结果
1) cout << "12345 " << setw(10) << setfill('$ ') << 10000;
2) cout << setw(8) << setprecision(3) << 1024.987654;
3) cout << oct << 99 << endl << hex << 99;
2,编写一程序实现温度从华氏到摄氏的转换,右端保留小数点两位,要有正负号表示,转换关系为:
celsius = 5.0/9.0 *(fahrenheit - 32),
其中华氏温度从0- 212。