C++面向对象程序设计项目教程项目七.ppt

bool find; //用find来检测是否找到 cout%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% endl; cout%%% %%%endl; cout%% 查询学生的考勤信息! %%endl; cout%%% %%%endl; cout%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% endl; cout请输入学生学号:endl; cinm; LABELQUERY: //用于goto语句跳转标记 try { find=false; //先设find为假,表示未找到 while(!find) { iofile.seekg(0,ios::beg); //定位于文件开头 iofile.read((char *)stud[i],sizeof(stud[i])); for(i=0;i5;i++) { if(m==stud[i].code) { cout学生学号:stud[i].codeendl; cout旷课日期:stud[i].dayendl; cout课程名:stud[i].coursenameendl; cout学生姓名:stud[i].nameendl; cout迟到:stud[i].style1endl; cout早退:stud[i].style2endl; cout请假:stud[i].style3endl; cout旷课:stud[i].style4endl; cout按回车键退出!endl; find=true; //表示找到记录 exit(1);}} throw(-1); } } //抛出异常信息 catch(int) //捕获整型异常信息 { cout该记录不存在!endl; cout你可以输入0结束查询,也可以重新输入学生学号进行查询:endl; cinm; if(m) //若输入值不为0,则继续查询 goto LABELQUERY; } iofile.close(); //关闭文件 getchar(); return(0); } 步骤2 经编译、连接后,程序运行结果如图所示。 Thank you! 任务实施——显示学生的个人信息与购书信息 案例说明 在两个不同的头文件中定义名字相同而功能不同的两个类,其中一个类表示学生的个人信息,另一个类表示学生的购书信息。编写程序,通过调用两个头文件中的同名类显示学生的个人信息和购书信息,实现在同一程序中使用同名的类。 案例分析 在两个头文件中分别定义的学生个人信息类和购书信息类,类名均为Student,但它们实现功能不同。将这两个类放在不同的命名空间head1与head2中。当要在主程序中调用这两个类时,先要将两个头文件包含到主程序中,然后通过在类名前使用不同命名空间名和作用域限定符来区分它们。 实施步骤 步骤1 在Visual C++中新建一个C++ 源文件,根据分析编写代码如下: //A_head1.h(头文件) #include string #include iostream using namespace std; namespace head1 { class Student //学生个人信息 { public: void show() {cout学号:numendl; cout学生姓名:nameendl;} Student(){} Student(int n,string s) { num=n;name=s; } private: int num; //学号 string name; }; //学生姓名 void fun() {coutStudent information.endl;}} //B_head2.h(头文件) #include string #includeiostream using namespace std; namespace head2 { class Student //学生购书信息 { private: int num; //书号 string name; //书名 float price; //定价 public: Student(int n,string nam,float p) {num=n;name=nam;price=p;} void show();}; void Student::show() { cout书号:numendl; cout书名:nameendl;} void fun() {coutStudents book info

文档评论(0)

1亿VIP精品文档

相关文档