1、二级 C+分类模拟 125 及答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.请使用 VC6 或使用【答题】菜单打开 proj1 下的工程 proj1。程序中位于每个“/ ERROR *found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: Name:Smith Age:21 ID:99999 CourseNum:12 Record:970 注意:只修改每个“/ ERROR *found*”下的那一行,不要改动程序中的其他内容。 #include iostream using namespace std; class S
2、tudentInfo protected: / ERROR *found* char Name; int Age; int ID; int CourseNum; float Record; public: StudentInfo(char * name, int Age, int ID, int courseNum, float record); / ERROR *found* void StudentInfo() float AverageRecord() return Record/CourseNum; void show() const cout “Name:“ Name “Age:“
3、Age “ID:“ ID “CourseNum:“ CourseNum “Record:“ Record endl; ; / ERROR *found* StudentInfo StudentInfo (char * Name, int Age, int ID, int CourseNum, float Record) Name = name; Age = age; this - ID = ID; CourseNum = courseNum; Record = record; int main() StudentInfo st (“Smith“,21,99999,12,970); st.sho
4、w(); return 0; (分数:30.00)_二、简单应用题(总题数:1,分数:30.00)2.请使用 VC6 或使用【答题】菜单打开 proj2 下的工程 proj2,其中定义了 vehicle 类,并派生出motorcar 类和 bicycle 类。然后以 motorcar 和 bicycle 作为基类,再派生出 motorcycle 类。要求将vehicle 作为虚基类,避免二义性问题。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为: 80 150 100 1 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“/
5、*found*”。 #include iostream.h class vehicle private: int MaxSpeed; int Weight; public: / *found* vehicle (int maxspeed, int weight):_ vehicle() ; int getMaxSpeed() return MaxSpeed; int getWeight() return Weight; ; / *found* class bicycle:_ public vehicle private: int Height; public: bicycle (int max
6、speed, int weight, int height): vehicle (maxspeed, weight), Height (height) int getHeight () return Height; ; / *found* class motorcar:_ public vehicle private: int SeatNum; public: motorcar (int maxspeed, int weight, int seatnum):vehicle (maxspeed, weight), SeatNum (seatnum) int getSeatNum() return
7、 SeatNum; ; / *found* class motorcycle:_ public: motorcycle(int maxspeed, int weight, int height): vehicle (maxspeed, weight), bicycle (maxspeed, weight, height), motorcar (maxspeed, weight, 1) ; void main() motorcycle a(80,150,100); cout a.getMaxSpeed() endl; cout a.getWeight() endl; cout a.getHeig
8、ht() endl; cout a.getSeatNum() endl; (分数:30.00)_三、综合应用题(总题数:1,分数:40.00)3.请使用 VC6 或使用【答题】菜单打开 proj3 下的工程 proj3,其中声明的 CDeepCopy 是一个用于表示矩阵的类。请编写这个类的赋值运算符成员函数 operator=,以实现深层复制。 要求: 补充编制的内容写在“/ *333*”与“/ *666*”之间。不得修改程序的其他部分。 注意:程序最后将结果输出到文件 out.dat 中。输出函数 writeToFile 已经编译为 obj 文件,并且在本程序中调用。 /CDeepCopy.
9、h #include iostream #include string using namespace std; class CDeepCopy public: int n; /动态数组的元素个数 int *p; /动态数组首地址 CDeepCopy(int); CDeepCopy(); CDeepCopy /赋值运算符函数 ; void writeToFile(char *); /main.cpp #include “CDeepCopy.h“ CDeepCopy:CDeepCopy() delete p; CDeepCopy:CDeepCopy(int k) n=k; p=new intn;
10、 /构造函数实现 CDeepCopy a.p0 = 1; d.p0 = 666; /对象 a,d 数组元算的赋值 CDeepCopy b(3); a.p0 = 88; b = a; /调用赋值运算符函数 cout b.p0; /显示内层局部对象的数组元素 cout d.p0; /显示 d 数组元算 a.p0的值 cout “d fade away; /n“; cout a.p0; /显示 a 数组元素 a.p0的值 writeToFile(“ “); return 0; (分数:40.00)_二级 C+分类模拟 125 答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数
11、:1,分数:30.00)1.请使用 VC6 或使用【答题】菜单打开 proj1 下的工程 proj1。程序中位于每个“/ ERROR *found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: Name:Smith Age:21 ID:99999 CourseNum:12 Record:970 注意:只修改每个“/ ERROR *found*”下的那一行,不要改动程序中的其他内容。 #include iostream using namespace std; class StudentInfo protected: / ERROR *found* char Name; int
12、 Age; int ID; int CourseNum; float Record; public: StudentInfo(char * name, int Age, int ID, int courseNum, float record); / ERROR *found* void StudentInfo() float AverageRecord() return Record/CourseNum; void show() const cout “Name:“ Name “Age:“ Age “ID:“ ID “CourseNum:“ CourseNum “Record:“ Record
13、 endl; ; / ERROR *found* StudentInfo StudentInfo (char * Name, int Age, int ID, int CourseNum, float Record) Name = name; Age = age; this - ID = ID; CourseNum = courseNum; Record = record; int main() StudentInfo st (“Smith“,21,99999,12,970); st.show(); return 0; (分数:30.00)_正确答案:()解析:(1)char*Name; (2
14、)StudentInfo() (3)StudentInfo:StudentInfo (char*name,int age,int ID,int courseNum,float record) 答案考生文件夹 考点 本题考查 StudentInfo 类,其中涉及构造函数、动态数组、析构函数和成员函数。声明数组时要指定数组的大小,否则将会导致程序出错,不论是构造函数还是析构函数都不能在函数名前添加返回类型。 解析 (1)主要考查考生对动态数组的掌握,由题目可知 Name 应该指向一个动态数组,而不是一个有效char 型字符,因此要定义成 char 型指针。 (2)主要考查考生对析构函数的掌握,析构
15、函数不需要函数返回类型,应把 void 去掉。 (3)主要考查考生对构造函数定义的掌握,构造函数也要使用作用域符号“:”。二、简单应用题(总题数:1,分数:30.00)2.请使用 VC6 或使用【答题】菜单打开 proj2 下的工程 proj2,其中定义了 vehicle 类,并派生出motorcar 类和 bicycle 类。然后以 motorcar 和 bicycle 作为基类,再派生出 motorcycle 类。要求将vehicle 作为虚基类,避免二义性问题。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为: 80 150 100 1 注意:只能在
16、横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“/ *found*”。 #include iostream.h class vehicle private: int MaxSpeed; int Weight; public: / *found* vehicle (int maxspeed, int weight):_ vehicle() ; int getMaxSpeed() return MaxSpeed; int getWeight() return Weight; ; / *found* class bicycle:_ public vehicle private: i
17、nt Height; public: bicycle (int maxspeed, int weight, int height): vehicle (maxspeed, weight), Height (height) int getHeight () return Height; ; / *found* class motorcar:_ public vehicle private: int SeatNum; public: motorcar (int maxspeed, int weight, int seatnum):vehicle (maxspeed, weight), SeatNu
18、m (seatnum) int getSeatNum() return SeatNum; ; / *found* class motorcycle:_ public: motorcycle(int maxspeed, int weight, int height): vehicle (maxspeed, weight), bicycle (maxspeed, weight, height), motorcar (maxspeed, weight, 1) ; void main() motorcycle a(80,150,100); cout a.getMaxSpeed() endl; cout
19、 a.getWeight() endl; cout a.getHeight() endl; cout a.getSeatNum() endl; (分数:30.00)_正确答案:()解析:(1)MaxSpeed(maxspeed),Weight(weight); (2)virtual (3)virtual (4)public bicycle,public motorcar 答案考生文件夹 考点 本题考查 vehicle 类及其派生类 motorcar 和 bicycle,再由这两类派生出motorcycle 类,其中涉及构造函数、成员函数和析构函数。构造函数采用成员初始化列表来完成对私有成员的初
20、始化,派生类的书写要注意关键字。 解析 (1)主要考查考生对构造函数的掌握,构造函数使用初始化列表来对私有成员 MaxSpeed 和 Weight初始化。 (2)主要考查考生对派生类的掌握,题目要求将 vehicle 作为虚基类,避免二义性问题。因此在这里添加virtual 使 vehicle 成为虚基类。 (3)主要考查考生对派生类的掌握,题目要求以 motorcar 和 bicycle 作为基类,再派生出 motorcycle 类。在主函数中可以看到 motorcycle 类的实例 a 调用 getHeight 函数和 getSeatNum 函数,由此可知这两个基类都是公有继承,因此得出语
21、句:public bicycle,public motorcar。 虚基类继承时要添加关键字 virtual,以避免二义性。三、综合应用题(总题数:1,分数:40.00)3.请使用 VC6 或使用【答题】菜单打开 proj3 下的工程 proj3,其中声明的 CDeepCopy 是一个用于表示矩阵的类。请编写这个类的赋值运算符成员函数 operator=,以实现深层复制。 要求: 补充编制的内容写在“/ *333*”与“/ *666*”之间。不得修改程序的其他部分。 注意:程序最后将结果输出到文件 out.dat 中。输出函数 writeToFile 已经编译为 obj 文件,并且在本程序中调
22、用。 /CDeepCopy.h #include iostream #include string using namespace std; class CDeepCopy public: int n; /动态数组的元素个数 int *p; /动态数组首地址 CDeepCopy(int); CDeepCopy(); CDeepCopy /赋值运算符函数 ; void writeToFile(char *); /main.cpp #include “CDeepCopy.h“ CDeepCopy:CDeepCopy() delete p; CDeepCopy:CDeepCopy(int k) n=
23、k; p=new intn; /构造函数实现 CDeepCopy a.p0 = 1; d.p0 = 666; /对象 a,d 数组元算的赋值 CDeepCopy b(3); a.p0 = 88; b = a; /调用赋值运算符函数 cout b.p0; /显示内层局部对象的数组元素 cout d.p0; /显示 d 数组元算 a.p0的值 cout “d fade away; /n“; cout a.p0; /显示 a 数组元素 a.p0的值 writeToFile(“ “); return 0; (分数:40.00)_正确答案:()解析:n = r.n; /把对象 r 字符长度赋值给 n d
24、elete p; /删除动态数组 p p = new intn; /给动态数组 p 分配空间为 n for (int i = 0; i n; i+) /遍历对象 r 中的数组 p pi = r.pi; /把 r.pi赋值给 pi return * this; /返回被赋值的对象 答案考生文件夹 考点 本题考查 CDeepCopy 类,其中涉及动态数组、构造函数、析构函数和运算符重载。运算符重载要先把类的定义弄清,其次要理解被重载的运算符的含义。 解析 主要考查考生对运算符重载的掌握,由注释可知此处要实现赋值运算符函数。要重载的运算符是“=”,该类的成员是动态数组 p,数组元素个数为 n,因此先释放原来的动态数组,再分配空间,然后逐个复制元素即可。