4. (20 scores)#
点击查看考点
类的基础语法, 类层次, 虚函数调用, 资源
Programming for under graduate students graduation audits: read basic information, according to the requirements of graduation audit, output the information into studentGradutate.data
file.
Among them, under graduate students are required to complete at least 150 credits, and graduate students are required to complete at least 36 credits, and pass both teaching practice and scientific research assessement.
The contents of the question include:
UStudent
1string strId; // Student number 2string strNames; // Student name 3double dCredits; // Credits completed 4bool bResult; // Can graduate or not
GStudent
1string strId; // Student number 2string strNames; // Student name 3double dCredits; // Credits completed 4bool bResult; // Can graduate or not 5bool bTeaching; // Pass teaching practice or not 6bool bResearch; // Pass scientific research assessement or not
Complete the following classes called Student
, UStudent
, GStudent
, and the main
function, so that the program can create a studentGraduate.data
file as following correctly:
1#include <fstream>
2#include <string>
3#include <vector>
4
5class Student {
6 public:
7 // ...
8
9 private:
10 std::string strId;
11 std::string strName;
12};
13
14class UStudent : public Student {
15 public:
16 // ...
17
18 private:
19 double dCredits;
20 bool bResult;
21};
22
23class GStudent : public UStudent {
24 public:
25 // ...
26
27 private:
28 double bTeaching;
29 bool bResearch;
30};
31
32int main() {
33 std::vector<Student*> s(4);
34 s[0] = new UStudent(1001, "Donald", 156, 1);
35 s[1] = new UStudent(1002, "Vivion", 142, 0);
36 s[2] = new GStudent(2003, "Chris", 36, 1, 1, 1);
37 s[3] = new GStudent(2004, "Cindy", 38, 1, 1, 1);
38
39 // output the information of students in to "studentGraduate.data" file
40 //according to the bResult data
41
42 { /* fill the blanks here */ }
43
44 for (int i = 0; i < s.size(); i++) {
45 delete s[i];
46 }
47}
11001 Donald 156 1
22003 Chris 36 1 1 1
32004 Cindy 38 1 1 1
翻译
编写实现学生的毕业审查: 读取基本信息, 根据毕业审查要求, 输出信息到 studentGradutate.data
文件中.
其中, 本科学生要求修满至少 150 学分; 研究生要求修满至少 36 学分, 并通过教学实践和科研实践考核.
该问题涉及以下内容:
UStudent
1string strId; // 学号 2string strNames; // 学生名字 3double dCredits; // 完成的学分数 4bool bResult; // 能否毕业
GStudent
1string strId; // 学号 2string strNames; // 学生名字 3double dCredits; // 完成的学分数 4bool bResult; // 能否毕业 5bool bTeaching; // 是否通过教学实践 6bool bResearch; // 是否通过科研实践
完成以下类 Student
, UStudent
, GStudent
和 main
函数的编写, 以让程序创建一个 studentGraduate.data
文件含有以下内容:
1#include <fstream>
2#include <string>
3#include <vector>
4
5class Student {
6 public:
7 // ...
8
9 private:
10 std::string strId;
11 std::string strName;
12};
13
14class UStudent : public Student {
15 public:
16 // ...
17
18 private:
19 double dCredits;
20 bool bResult;
21};
22
23class GStudent : public UStudent {
24 public:
25 // ...
26
27 private:
28 double bTeaching;
29 bool bResearch;
30};
31
32int main() {
33 std::vector<Student*> s(4);
34 s[0] = new UStudent(1001, "Donald", 156, 1);
35 s[1] = new UStudent(1002, "Vivion", 142, 0);
36 s[2] = new GStudent(2003, "Chris", 36, 1, 1, 1);
37 s[3] = new GStudent(2004, "Cindy", 38, 1, 1, 1);
38
39 // 根据 bResult 数据, 将学生输出到 "studentGraduate.data" 文件中
40
41 { /* 补全此处的代码 */ }
42
43 for (int i = 0; i < s.size(); i++) {
44 delete s[i];
45 }
46}
11001 Donald 156 1
22003 Chris 36 1 1 1
32004 Cindy 38 1 1 1
点击查看解答参考
这道题注意一下, 21 年最后一道题也考了类似的题。
此题中确切给出了输出文件的输出内容, 故虽然输出文件以看似很二进制的 .data
为后缀, 但不是二进制输出.