c++ io标准库

我们从一开始就一直在利用C++的输入输出在做着各种练习,输入输出是由iostream库提供的,所以讨论此标准库是有必要的,它与C语言的stdio库不同,它从一开始就是用多重继承与虚拟继承实现的面向对象的层次结构,作为一个c++的标准库组件提供给程序员使用。

  iostream为内置类型类型对象提供了输入输出支持,同时也支持文件的输入输出,类的设计者可以通过对iostream库的扩展,来支持自定义类型的输入输出操作。

  为什么说要扩展才能提供支持呢?我们来一个示例。

#include <stdio.h>
#include <iostream>
using namespace std;

class Test
{
public:
Test(int a=0,int b=0)
{
Test::a=a;
Test::b=b;
}
int a;
int b;
};
int main()
{
Test t(100,50);
printf("%???",t);//不明确的输出格式
scanf("%???",t);//不明确的输入格式
cout<<t<<endl;//同样不够明确
cin>>t;//同样不够明确
system("pause");
}

  由于自定义类的特殊性,在上面的代码中,无论你使用c风格的输入输出,或者是c++的输入输出都不是不明确的一个表示,由于c语言没有运算符重载机制,导致stdio库的不可扩充性,让我们无法让printf()和scanf()支持对自定义类对象的扩充识别,而c++是可以通过运算符重载机制扩充iostream库的,使系统能能够识别自定义类型,从而让输入输出明确的知道他们该干什么,格式是什么。

  在上例中我们之所以用printf与cout进行对比目的是为了告诉大家,C与C++处理输入输出的根本不同,我们从c远的输入输出可以很明显看出是函数调用方式,而c++的则是对象模式,cout和cin是ostream类和istream类的对象

  C++中的iostream库主要包含下图所示的几个头文件:

c++ io标准库

  我们所熟悉的输入输出操作分别是由istream(输入流)和ostream(输出流)这两个类提供的,为了允许双向的输入/输出,由istream和ostream派生出了iostream类。

  类的继承关系见下图:

c++ io标准库

iostream库定义了以下三个标准流对象:

  1.cin,表示标准输入(standard input)的istream类对象。cin使我们可以从设备读如数据。
  2.cout,表示标准输出(standard output)的ostream类对象。cout使我们可以向设备输出或者写数据。
  3.cerr,表示标准错误(standard error)的osttream类对象。cerr是导出程序错误消息的地方,它只能允许向屏幕设备写数据

  输出主要由重载的左移操作符(<<)来完成,输入主要由重载的右移操作符(>>)完成。

  >>a表示将数据放入a对象中。
  <<a表示将a对象中存储的数据拿出。

  这些标准的流对象都有默认的所对应的设备,见下表:

c++ io标准库

  图中的意思表明cin对象的默认输入设备是键盘,cout对象的默认输出设备是显示器屏幕。

  那么原理上C++有是如何利用cin/cout对象与左移和右移运算符重载来实现输入输出的呢?

  下面我们以输出为例,说明其实现原理

  cout是ostream类的对象,因为它所指向的是标准设备(显示器屏幕),所以它在iostream头文件中作为全局对象进行定义。

  ostream cout(stdout);//其默认指向的C中的标准设备名,作为其构造函数的参数使用。 (#add 此定义在vs中未找到,待证实)

  在iostream.h头文件中,ostream类对应每个基本数据类型都有其友元函数对左移操作符进行了友元函数的重载。
  ostream& operator<<(ostream &temp,int source);
  ostream& operator<<(ostream &temp,char *ps);
  。。。。等等

  一句输出语句:cout<<"www.cndev-lab.com";,事实上调用的就是ostream& operator<<(ostream &temp,char *ps);这个运算符重载函数,由于返回的是流对象的引用,引用可以作为左值使用,所以当程序中有类似cout<<"www.cndev-lab.com"<<"中国软件开发实验室";这样的语句出现的时候,就能够构成连续输出。

  由于iostream库不光支持对象的输入输出,同时也支持文件流的输入输出,所以在详细讲解左移与右移运算符重载只前,我们有必要先对文件的输入输出以及输入输出的控制符有所了解。

  和文件有关系的输入输出类主要在fstream.h这个头文件中被定义,在这个头文件中主要被定义了三个类,由这三个类控制对文件的各种输入输出操作,他们分别是ifstream、ofstream、fstream,其中fstream类是由iostream类派生而来,他们之间的继承关系见下图所示。

c++ io标准库

  由于文件设备并不像显示器屏幕与键盘那样是标准默认设备,所以它在fstream.h头文件中是没有像cout那样预先定义的全局对象,所以我们必须自己定义一个该类的对象,我们要以文件作为设备向文件输出信息(也就是向文件写数据),那么就应该使用ofstream类。

  ofstream类的默认构造函数原形为:
  ofstream::ofstream(const char *filename,int mode = ios::out,int openprot = filebuf::openprot);
  filename:  要打开的文件名
  mode:    要打开文件的方式
  openprot:  打开文件的属性

  其中mode和openprot这两个参数的可选项表见下表:
mode属性表
  ios::app:   以追加的方式打开文件
  ios::ate:   文件打开后定位到文件尾,ios:app就包含有此属性
  ios::binary:  以二进制方式打开文件,缺省的方式是文本方式。两种方式的区别见前文
  ios::in:    文件以输入方式打开
  ios::out:   文件以输出方式打开
  ios::trunc:  如果文件存在,把文件长度设为0

  可以用“|”把以上属性连接起来,如ios::out | ios::binary。

openprot属性表:
  0:普通文件,打开访问
  1:只读文件
  2:隐含文件
  4:系统文件

可以用“|”或者“+”把以上属性连接起来 ,如3或 1 | 2 就是以只读和隐含属性打开文件。
#include <fstream>
using namespace std;

int main()
{
ofstream myfile("c:\1.txt",ios::out|ios::trunc,0);
myfile<<"中国软件开发实验室"<<endl<<"网址:"<<"www.cndev-lab.com";
myfile.close();
system("pause");
}

文件使用完后可以使用close成员函数关闭文件。 (#add代码运行出错)

ios::app为追加模式,在使用追加模式的时候同时进行文件状态的判断是一个比较好的习惯。
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream myfile("c:\1.txt",ios::app,0);
if(!myfile)//或者写成myfile.fail()
{
cout<<"文件打开失败,目标文件状态可能为只读!";
system("pause");
exit(1);
}
myfile<<"中国软件开发实验室"<<endl<<"网址:"<<"www.cndev-lab.com"<<endl;
myfile.close();
}

 

在定义ifstream和ofstream类对象的时候,我们也可以不指定文件。以后可以通过成员函数open()显式的把一个文件连接到一个类对象上。
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream myfile;
myfile.open("c:\1.txt",ios::out|ios::app,0);
if(!myfile)//或者写成myfile.fail()
{
cout<<"文件创建失败,磁盘不可写或者文件为只读!";
system("pause");
exit(1);
}
myfile<<"中国软件开发实验室"<<endl<<"网址:"<<"www.cndev-lab.com"<<endl;
myfile.close();
}

 

下面我们来看一下是如何利用ifstream类对象,将文件中的数据读取出来,然后再输出到标准设备中的例子。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream myfile;
myfile.open("c:\1.txt",ios::in,0);
if(!myfile)
{
cout<<"文件读错误";
system("pause");
exit(1);
}
char ch;
string content;
while(myfile.get(ch))
{
content+=ch;
cout.put(ch);//cout<<ch;这么写也是可以的
}
myfile.close();
cout<<content;
system("pause");
}

  上例中,我们利用成员函数get(),逐一的读取文件中的有效字符,再利用put()成员函数,将文件中的数据通过循环逐一输出到标准设备(屏幕)上,get()成员函数会在文件读到默尾的时候返回假值,所以我们可以利用它的这个特性作为while循环的终止条件,我们同时也在上例中引入了C++风格的字符串类型string,在循环读取的时候逐一保存到content中,要使用string类型,必须包含string.h的头文件。

 

我们在简单介绍过ofstream类和ifstream类后,我们再来看一下fstream类,fstream类是由iostream派生而来,fstream类对象可以同对文件进行读写操作。

示例代码如下:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream myfile;
myfile.open("c:\1.txt",ios::out|ios::app,0);
if(!myfile)
{
cout<<"文件写错误,文件属性可能为只读!"<<endl;
system("pause");
exit(1);
}
myfile<<"中国软件开发实验室"<<endl<<"网址:"<<"www.cndev-lab.com"<<endl;
myfile.close();

myfile.open("c:\1.txt",ios::in,0);
if(!myfile)
{
cout<<"文件读错误,文件可能丢失!"<<endl;
system("pause");
exit(1);
}
char ch;
while(myfile.get(ch))
{
cout.put(ch);
}
myfile.close();
system("pause");
}

 由于fstream类可以对文件同时进行读写操作,所以对它的对象进行初始话的时候一定要显式的指定mode和openprot参数。

 接下来我们来学习一下串流类的基础知识,什么叫流(#add 字符串流)类
  简单的理解就是能够控制字符串类型对象进行输入输出的类,C++不光可以支持C++风格的字符串流控制,还可以支持C风格的字符串流控制。

  我们先看看看C++是如何对C风格的字符串流进行控制的,C中的字符串其实也就是字符数组,字符数组内的数据在内存中的位置的排列是连续的,我们通常用char str[size]或者char *str的方式声明创建C风格字符数组,为了能让字符数组作为设备并提供输入输出操作,C++引入了ostrstream、istrstream、strstream这三个类,要使用他们创建对象就必须包含strstream.h头文件。
  istrstream类用于执行C风格的串流的输入操作,也就是以字符串数组作为输入设备。
  ostrstream类用于执行C风格的串流的输出操作,也就是一字符串数组作为输出设备。
  strstream类同时可以支持C风格的串流的输入输出操作。

  istrstream类是从istream(输入流类)和strstreambase(字符串流基类)派生而来,ostrstream是从ostream(输出流类)和strstreambase(字符串流基类)派生而来,strstream则是从iostream(输入输出流类)和和strstreambase(字符串流基类)派生而来。

  他们的继承关系如下图所示:

c++ io标准库

  串流同样不是标准设备,不会有预先定义好的全局对象,所以不能直接操作,需要通过构造函数创建对象。

类istrstream的构造函数原形如下:
  istrstream::istrstream(const char *str,int size);
  参数1表示字符串数组,而参数2表示数组大小,当size为0时,表示istrstream类对象直接连接到由str所指向的内存空间并以结尾的字符串。

下面的示例代码就是利用istrstream类创建类对象,制定流输入设备为字符串数组,通过它向一个字符型对象输入数据。
#include <iostream>
#include <strstream>
using namespace std;
int main()
{
char *name = "www.cndev-lab.com";
int arraysize = strlen(name)+1;
istrstream is(name,arraysize);
char temp;
is>>temp;
cout<<temp;
system("pause");
}

  类ostrstream用于执行串流的输出,它的构造函数如下所示:
  ostrstream::ostrstream(char *_Ptr,int streamsize,int Mode = ios::out);
  第一个参数是字符数组,第二个是说明数组的大小,第三个参数是指打开方式。

#include <iostream>
#include <strstream>
using namespace std;
int main()
{
int arraysize=1;
char *pbuffer=newchar[arraysize];
ostrstream ostr(pbuffer,arraysize,ios::out);
ostr<<arraysize<<ends;//使用ostrstream输出到流对象的时候,要用ends结束字符串
cout<<pbuffer;
delete[] pbuffer;
system("pause");
}

  上面的代码中,我们创建一个c风格的串流输出对象ostr,我们将arraysize内的数据成功的以字符串的形式输出到了ostr对象所指向的pbuffer指针的堆空间中,pbuffer也正是我们要输出的字符串数组,在结尾要使用ends结束字符串,如果不这么做就有溢出的危险。

接下来我们继续看一下C++风格的串流控制,C++引入了ostringstream、istringstream、stringstream这三个类,要使用他们创建对象就必须包含sstream.h头文件。

  istringstream类用于执行C++风格的串流的输入操作。
  stringstream类同时可以支持C++风格的串流的输入输出操作。
  strstream类同时可以支持C风格的串流的输入输出操作。

  istringstream类是从istream(输入流类)和stringstreambase(c++字符串流基类)派生而来,ostringstream是从ostream(输出流类)和stringstreambase(c++字符串流基类)派生而来,stringstream则是从iostream(输入输出流类)和和stringstreambase(c++字符串流基类)派生而来。

  他们的继承关系如下图所示:

c++ io标准库

  istringstream是由一个string对象构造而来,istringstream类从一个string对象读取字符。
  istringstream的构造函数原形如下:
  istringstream::istringstream(string str);
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
istringstream istr;
istr.str("1 56.7",);
//上述两个过程可以简单写成 istringstream istr("1 56.7");
cout << istr.str()<<endl;
int a;
float b;
istr>>a;
cout<<a<<endl;
istr>>b;
cout<<b<<endl;
system("pause");
}

  上例中,构造字符串流的时候,空格会成为字符串参数的内部分界,例子中对a,b对象的输入"赋值"操作证明了这一点,字符串的空格成为了整型数据与浮点型数据的分解点,利用分界获取的方法我们事实上完成了字符串到整型对象与浮点型对象的拆分转换过程。

  str()成员函数的使用可以让istringstream对象返回一个string字符串(例如本例中的输出操作(cout<<istr.str();)。

  ostringstream同样是由一个string对象构造而来,ostringstream类向一个string插入字符。
  ostringstream的构造函数原形如下:
  ostringstream::ostringstream(string str);

#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
ostringstream ostr;
//ostr.str("abc");//如果构造的时候设置了字符串参数,那么增长操作的时候不会从结尾开始增加,而是修改原有数据,超出的部分增长
ostr.put('d');
ostr.put('e');
ostr<<"fg";

string gstr = ostr.str();
cout<<gstr;
system("pause");
}

  在上例代码中,我们通过put()或者左移操作符可以不断向ostr插入单个字符或者是字符串,通过str()函数返回增长过后的完整字符串数据,但值得注意的一点是,当构造的时候对象内已经存在字符串数据的时候,那么增长操作的时候不会从结尾开始增加,而是修改原有数据,超出的部分增长。

对于stringstream了来说,不用我多说,大家也已经知道它是用于C++风格的字符串的输入输出的。
  stringstream的构造函数原形如下:
  stringstream::stringstream(string str);
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
stringstream ostr("ccc");
ostr.put('d');
ostr.put('e');
ostr<<"fg";
string gstr = ostr.str();
cout<<gstr<<endl;

char a;
ostr>>a;
cout<<a

system("pause");
}

 

除此而外,stringstream类的对象我们还常用它进行string与各种内置类型数据之间的转换。
#include <iostream>
#include <sstream>
#include <string>
usingnamespace std;

intmain()
{
stringstream sstr;
//--------int转string-----------
int a=100;
string str;
sstr<<a;
sstr>>str;
cout<<str<<endl;
//--------string转char[]--------
sstr.clear();//如果你想通过使用同一stringstream对象实现多种类型的转换,请注意在每一次转换之后都必须调用clear()成员函数。
string name = "colinguan";
char cname[200];
sstr<<name;
sstr>>cname;
cout<<cname;
system("pause");
}

  接下来我们来学习一下输入/输出的状态标志的相关知识,C++中负责的输入/输出的系统包括了关于每一个输入/输出操作的结果的记录信息。这些当前的状态信息被包含在io_state类型的对象中。io_state是一个枚举类型(就像open_mode一样),以下便是它包含的值。

goodbit 无错误
Eofbit 已到达文件尾
failbit 非致命的输入/输出错误,可挽回
badbit 致命的输入/输出错误,无法挽回

有两种方法可以获得输入/输出的状态信息。一种方法是通过调用rdstate()函数,它将返回当前状态的错误标记。例如,假如没有任何错误,则rdstate()会返回goodbit.

下例示例,表示出了rdstate()的用法:
#include <iostream>
using namespace std;

int main()
{
int a;
cin>>a;
cout<<cin.rdstate()<<endl;
if(cin.rdstate() == ios::goodbit)
{
cout<<"输入数据的类型正确,无错误!"<<endl;
}
if(cin.rdstate() == ios_base::failbit)
{
cout<<"输入数据类型错误,非致命错误,可清除输入缓冲区挽回!"<<endl;
}
system("pause");
}

  另一种方法则是使用下面任何一个函数来检测相应的输入/输出状态:

bool bad();
bool eof();
bool fail();
bool good();

下例示例,表示出了上面各成员函数的用法:
#include <iostream>
using namespace std;

int main()
{
int a;
cin>>a;
cout<<cin.rdstate()<<endl;
if(cin.good())
{
cout<<"输入数据的类型正确,无错误!"<<endl;
}
if(cin.fail())
{
cout<<"输入数据类型错误,非致命错误,可清除输入缓冲区挽回!"<<endl;
}
system("pause");
}

  

如果错误发生,那么流状态既被标记为错误,你必须清除这些错误状态,以使你的程序能正确适当地继续运行。要清除错误状态,需使用clear()函数。此函数带一个参数,它是你将要设为当前状态的标志值。,只要将ios::goodbit作为实参。
#include <iostream>
using namespace std;

int main()
{
int a;
cin>>a;
cout<<cin.rdstate()<<endl;
cin.clear(ios::goodbit);
cout<<cin.rdstate()<<endl;
system("pause");
}

 

通常当我们发现输入有错又需要改正的时候,使用clear()更改标记为正确后,同时也需要使用get()成员函数清除输入缓冲区,以达到重复输入的目的。
#include <iostream>
using namespace std;

int main()
{
int a;
while(1)
{
cin>>a;
if(!cin)//条件可改写为cin.fail()
{
cout<<"输入有错!请重新输入"<<endl;
cin.clear();
cin.get();
}
else
{
cout<<a;
break;
}
}
system("pause");
}

 

最后再给出一个对文件流错误标记处理的例子,巩固学习,代码如下:
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
ifstream myfile("c:\1.txt",ios_base::in,0);
if(myfile.fail())
{
cout<<"文件读取失败或指定文件不存在!"<<endl;
}
else
{
char ch;
while(myfile.get(ch))
{
cout<<ch;
}
if(myfile.eof())
{
cout<<"文件内容已经全部读完"<<endl;
}
while(myfile.get(ch))
{
cout<<ch;
}
}
system("pause");
}

C语言提供了格式化输入输出的方法,C++也同样,但是C++的控制符使用起来更为简单方便,在c++下有两中方法控制格式化输入输出。
  1.有流对象的成员函数。
  例如,下列程序以成员函数的方式控制输出的精度:
#include <iostream>
using namespace std;

int main()
{
float pi=3.14159f;
cout<<pi<<endl;
cout.precision(2);
cout<<pi<<endl;
system("pause");
}

  2.使用C++输入输出控制符,控制符是在拖文件iomanip.h中定义的对象,与成员函数有一样的效果,控制符不必像成员函数学那样单独调用,它可以直接插入流中使用。
  例如,下列程序以控制符的方式控制输出的精度:
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
float pi=3.14159f;
cout<<pi<<endl;
cout<<setprecision(4);
cout<<pi<<endl;
system("pause");
}

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

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

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

第一个参数是字符数组,用于存放整行文本,第二个参数读取的最大字符个数,第三个参数为作为分界界限的字符,默认识是n,换行符。
#include <iostream>
#include <iomanip>
using namespace std;

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

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

先说左移(<<)操作符,也就是我们常说的输出操作符
  

对于自定义类来说,重载左移操作符的方法我们常使用类的友元方式进行操作。
#include <iostream>
usingnamespace std;

class Test
{
public:
Test(int age = 0,char *name = "")
{
Test::age = age;
strcpy(Test::name,name);
}
void outmembers(ostream &out)
{
out<<"Age:"<<age<<endl<<"Name:"<<this->name<<endl;
}
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<<a;
system("pause");
}

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

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

  下面的示例代码就是将上面的<<重载函数修改成成员方式的做法:
#include <iostream>
usingnamespace std;

class Test
{
public:
Test(int age = 0,char *name = "")
{
Test::age = age;
strcpy(Test::name,name);
}
void outmembers(ostream &out)
{
out<<"Age:"<<age<<endl<<"Name:"<<this->name<<endl;
}
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<<cout;,这样一来代码的可读行就大大降低了,这到底是左移还是右移呢?为此我再一次说明,对于左移和右移运算符的重载是十分不推荐使用成员函数的方式编写的。

 

为了巩固学习,下面我们以fstream对象输出为例做一个练习。
#include <iostream>
#include <fstream>
usingnamespace std;

class Test
{
public:
Test(int age = 0,char *name = "")
{
Test::age = age;
strcpy(Test::name,name);
}
void outmembers(ostream &out)
{
out<<"Age:"<<age<<endl<<"Name:"<<this->name<<endl;
}
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<<a;
cout<<"文件创建成功,写入正常!"<<endl;
}
if (myfile.rdstate() == ios_base::badbit)
{
cout<<"文件创建失败,磁盘错误!"<<endl;
}
system("pause");
}

  

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

class Student
{
public:
Student(int age = 0,char *name = "")
{
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 = "",char *speciality=""):Student(age,name)
{
strcpy(Academician::speciality,speciality);
}
virtualvoid outmembers(ostream &out)
{
out<<"Age:"<<age<<endl<<"Name:"<<name<<endl<<
"speciality:"<<speciality<<endl;
}
protected:
char speciality[80];
};
class GraduateStudent:public Academician
{
public:
GraduateStudent(int age = 0,char *name = "",char *speciality="",
char *investigate=""):Academician(age,name,speciality)
{
strcpy(GraduateStudent::investigate,investigate);
}
virtualvoid outmembers(ostream &out)
{
out<<"Age:"<<age<<endl<<"Name:"<<name<<endl<<
"speciality:"<<speciality<<endl<<"investigate:"<<investigate<<endl;
}
protected:
char investigate[100];
};
intmain()
{
Academician a(24,"管宁","Computer Science");
cout<<a;
GraduateStudent b(24,"严燕玲","Computer Science","GIS System");
cout<<b;
system("pause");
}

  在上面的代码中为了能够区分输出a对象与b对象,我们用虚函数的方式重载了继承类Academician与多重继承类GraduateStudent的outmembers成员函数,由于ostream& operator <<(ostream& out,Student &temp) 运算符重载函数是Student基类的,Student &temp参数通过虚函数的定义可以适应不同派生类对象,所以在其内部调用temp.outmembers(out); 系统可识别不同继类的outmembers()成员函数。

 

最后看一下,右移运算符的重载,右移运算符我们也常叫它输入运算符号,对于它来说,具体实现和左移运算符的重载差别并不大,对于有多成员对象的类来说,只要保证能够完整输入各成员对象大数据就可以了。
#include <iostream>
usingnamespace std;

class Test
{
public:
Test(int age = 0,char *name = "")
{
Test::age = age;
strcpy(Test::name,name);
}
void inputmembers(istream &out)
{
cout<<"please input age:";
cin>>Test::age;
cout<<"please input name:";
cin>>Test::name;
}
friendistream& operator >>(istream& ,Test&);
public:
int age;
char name[50];
};
istream& operator >>(istream& input,Test &temp)
{
temp.inputmembers(input);
return input;
}
intmain()
{
Test a;
cin>>a;
cout<<a.age<<"|"<<a.name<<endl;
system("pause");
}

 

原文链接: https://www.cnblogs.com/weiqubo/archive/2011/06/10/2077710.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    c++ io标准库

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/27006

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 上午4:36
下一篇 2023年2月8日 上午4:37

相关推荐