c++ io标准库2(三)

2014-11-24 12:41:14 · 作者: · 浏览: 2
.h中定义的对象,与成员函数有一样的效果,控制符不必像成员函数学那样单独调用,它可以直接插入流中使用。
  例如,下列程序以控制符的方式控制输出的精度:

//程序作者:管宁
//站点:www.cndev-lab.com
//所有稿件均有版权,如要转载,请务必著名出处和作者

#include <iostream>
#include <iomanip>
usingnamespace std;

intmain()
{
float pi=3.14159f;
cout< cout< cout< system("pause");
}

  下表我们列出了一些比较常用的控制符号,由于篇幅有限读者请根据自己的需要查阅相关书籍:

  对于iostream标准库来说包含了众多的成员函数,各函数都有其自身的作用,篇幅问题笔者在这里不能一一说明例举,由于标准输入对象cin提供输入的时候会自动以空格作为分界,给我们获取一行带有空格的完整字符串带来了困难,在这里补充一个非常用有的成员函数----getline()。

  其函数原型为:
  getlin(chiar *str,int size,char='\n');

  第一个参数是字符数组,用于存放整行文本,第二个参数读取的最大字符个数,第三个参数为作为分界界限的字符,默认识是\n,换行符。

  示例代码如下:

//程序作者:管宁
//站点:www.cndev-lab.com
//所有稿件均有版权,如要转载,请务必著名出处和作者

#include <iostream>
#include <iomanip>
usingnamespace std;

intmain()
{
char str[100];
cin.getline(str,sizeof(str),'\n');
cout< system("pause");
}

  通过上面内容的学习,我们对i/o有了一些基本点基本的认识,现在是该切入正题的时候了,详细学习一下,如何重载左移与右移操作符。

先说左移(<<)操作符,也就是我们常说的输出操作符
  对于自定义类来说,重载左移操作符的方法我们常使用类的友元方式进行操作。


  示例代码如下:

//程序作者:管宁
//站点:www.cndev-lab.com
//所有稿件均有版权,如要转载,请务必著名出处和作者

#include <iostream>
usingnamespace std;

class Test
{
public:
Test(int age = 0,char *name = "\0")
{
Test::age = age;
strcpy(Test::name,name);
}
void outmembers(ostream &out)
{
out<<"Age:"<this->name< }
friendostream& operator <<(ostream& ,Test&);
protected:
int age;
char name[50];
};
ostream& operator <<(ostream& out,Test &temp)
{
temp.outmembers(out);
return out;
}
intmain()
{
Test a(24,"管宁");
cout< system("pause");
}

  上例代码中,我们对void outmembers(ostream &out)的参数使用ostream定义主要是为了可以向它传递任何ostream类对象不光是cout也可以是ofstrem或者是ostrstream和ostringstream类对象,做到通用性。

  重载运算符,我们知道可以是非成员方式也可以是成员方式的,对于<<来说同样也可以是成员方式,但我十分不推荐这么做,因为对于类的成员函数来说,第一个参数始终是会被隐藏的,而且一定是当前类类型的。

  下面的示例代码就是将上面的<<重载函数修改成成员方式的做法:

//程序作者:管宁
//站点:www.cndev-lab.com
//所有稿件均有版权,如要转载,请务必著名出处和作者

#include <iostream>
usingnamespace std;

class Test
{
public:
Test(int age = 0,char *name = "\0")
{
Test::age = age;
strcpy(Test::name,name);
}
void outmembers(ostream &out)
{
out<<"Age:"<this->name< }
ostream& operator <<(ostream &out)
{
this->outmembers(out);
return out;
}
protected:
int age;
char name[50];
};
intmain()
{
Test a(24,"管宁");
a<<cout;
system("pause");
}

  从代码实现上,我们将函数修改成了ostream& operator <<(ostream &out),迫不得已将ostream类型的引用参数放到了后面,这是因为,成员方式运算符重载函数第一个参数会被隐藏,而且一定是当前类类型的,这和ostream类型冲突了。由此我们在使用cout输出的时候就必须写成a<

为了巩固学习,下面我们以fstream对象输出为例做一个练习。

  代码如下:

//程序作者:管宁
//站点:www.cndev-lab.com
//所有稿件均有版权,如要转载,请务必著名出处和作者
#include <iostream>
#include <fstream>
usingnamespace std;

class Test
{
public:
Test(int age = 0,char *name = "\0")
{
Test::age = age;
strcpy(Test::name,name);
}
void outmembers(ostream &out)
{
out<<"Age:"<this->name< }
friendostream& operator <<(ostream& ,Test&);
protected:
int age;
char name[50];
};
ostream& operator <<(ostream& out,Test &temp)
{
temp.outmembers(out);
return out;
}
intmain()
{
Test a(24,"管宁");
ofstream myfile("c:\\1.txt",ios::out,0);
if (myfile.rdstate() == ios_base::goodbit)
{
myfile< cout<<"文件创建成功,写入正常!"< }
if (myfile.rdstate() == ios_base::badbit)
{
cout<<"文件创建失败,磁盘错误!"< }
system("pause");
}

  对于左移运算符重载函数来说,由于不推荐使用成员方式,那么使用非成员方式在类有多重继承的情况下,就不能使用虚函数进行左移运算符重载的区分,为了达到能够区分显示的目的,给每个类分别添加不同的虚函数是必要的。

  示例代码如下:

//程序作者:管宁
//站点:www.cndev-lab.com
//所有稿件均有版权,如要转载,请务必著名出处和作者

#include <iostream>
#include <fstream>
usingnamespace std;

class Student
{
public:
Student(int age = 0,char *name = "\0")
{
Student::age = age;
strcpy(Student::name,name);
}
virtualvoid outmembers(ostream &out) = 0;
friendostream& operator <<(ostream& ,Student&);
protected:
int age;
char name[50];
};
ostream& operator <<(ostream& out,Student &temp)
{
temp.outmembers(out);
return out;
}
class Academician:public Student
{
public:
Academician(int age = 0,char *name = "\0",char *specialit