第19章IO流试卷.ppt

//** student.h ** #include iostream.h #include string.h #ifndef STUDENT #define STUDENT class Student{ //大学生类 protected: char pName[20]; unsigned int uID; float grade; public: Student(char* pS, unsigned num, float g) { strcpy(pName,pS); uID=num; grade=g; } virtual void display(ostream out); }; ostream operator(ostream out, Student st); #endif //** student.cpp ** #include student.h #include iomanip.h #include iostream.h void Student::display(ostream out) { out setiosflags(ios::left)setw(20)pName uID , setiosflags(ios::right) setw(4)grade; } //插入操作符 ostream operator(ostream out, Student st) { st.display(out); out endl; return out; } //******************** //** master.h ** //******************** #include student.h #include iostream.h //硕士生类 class MasterStudent: public Student{ protected: char type; public: MasterStudent(char* pS, unsigned num, float g, char t) :Student(pS,num,g),type(t){} void display(ostream out); }; //master.cpp #include iostream.h #include master.h void MasterStudent::display(ostream out) { Student::display(out); out , type; } //*********************** //** ch19_15.cpp ** //*********************** #include fstream.h #include student.h #include master.h int main() { ofstream out(e:\\bctemp\\abc.txt); Student s1(Dill Arnson, 12567, 3.5); MasterStudent s2(Welch Shammas, 12667, 4.1, A); MasterStudent s3(Portel Braumbel, 12579, 3.8, B); out s1; out s2; out s3; } * 19.9 文件操作 运行结果可以在打开的文件中看到: e:\type abc.txt Dill Arnson 12567, 3.5 Welch Shammas 12667, 4.1, A Portel Braumbel 12579, 3.8, B * 19.9 文件操作 2.文件输入:读文件 如果要打开一个文件用于输入,可以用ifstream类。假定文件abc.txt中的内容为: Dill Arnson 12567,3.5 Welch Shammas 12667,4.1,A P

文档评论(0)

1亿VIP精品文档

相关文档