#include <ⅰ ostream.h> void maino char ch, str[81] cingetline(str, 81); cout << str <<"It'<< cin gcount( < endl cin > ch cout < ch < end:
#include <iostream.h> void main() { char ch, str[81]; cin.getline(str, 81); cout << str << '\t' << cin.gcount() << endl; cin >> ch; cout << ch << endl; }
13.3.3格式化输出 格式化输出是通过 ostream类的对象,利用重载了的“<<”运 算符来实现的。 最常用的输出流对象为cout,有时,出于某种特殊需要也会用 到cer和cog。程序员也可以定义自己的输出流类,但实用 价值不大 格式化输出的缺省格式为:十进制、域宽为0、左对齐、空格 填充;精度为6位小数、浮点数
13.3.3 格式化输出 格式化输出是通过 ostream 类的对象,利用重载了的 “<<” 运 算符来实现的。 最常用的输出流对象为 cout,有时,出于某种特殊需要也会用 到 cerr 和 clog。程序员也可以定义自己的输出流类,但实用 价值不大。 格式化输出的缺省格式为:十进制、域宽为 0、左对齐、空格 填充;精度为 6 位小数、浮点数
1334输出操作函数 ostream& ostream: put(char) ∥输出单个字符 ostream& ostream: flush( ∥刷新输出流 例 #include <iostream.h> void main( char ch=A while(ch <=Z put(ch ++ put(n);
13.3.4 输出操作函数 ostream& ostream :: put(char); // 输出单个字符 ostream& ostream :: flush(); // 刷新输出流 例: #include <iostream.h> void main() { char ch = 'A'; while(ch <= 'Z') put(ch ++) put('\n'); }
13.3.5重载提取和插入运算符 friend istream& operator >>(istream&, cls&); friend ostream& operator <<(ostream&, cls&); 其中:cs为运算符重载所属类的类名。 从上述运算符重载的一般形式可以看出:提取和插入运算符必 须重载为类的友元函数,且函数的返回值必须是对相应类的引 用。至于参数,除了要求必须有一个流和一个类(说明运算符 重载的类)外,对其类型无严格要求。也就是说,可以是引用、 对象甚至指针。但习惯上常用引用,几乎从不使用指针
13.3.5 重载提取和插入运算符 friend istream& operator >> (istream&, cls&); friend ostream& operator << (ostream&, cls&); 其中:cls 为运算符重载所属类的类名。 从上述运算符重载的一般形式可以看出:提取和插入运算符必 须重载为类的友元函数,且函数的返回值必须是对相应类的引 用。至于参数,除了要求必须有一个流和一个类(说明运算符 重载的类)外,对其类型无严格要求。也就是说,可以是引用、 对象甚至指针。但习惯上常用引用,几乎从不使用指针
include <iostream. h> class×{ int x public X(int a=0): X(a 1 int Get( return X; 3 void Set(int a) I x= a friend ostream& operator <<(ostream&, X&) friend istream& operator >>(istream&, X&)
#include <iostream.h> class X { int x; public: X(int a = 0) : x(a) {} int Get() { return x; } void Set(int a) { x = a; } friend ostream& operator << (ostream&, X&); friend istream& operator >> (istream&, X&); };