【计算机类职业资格】C++语言笔试-8及答案解析.doc

上传人:figureissue185 文档编号:1318226 上传时间:2019-10-17 格式:DOC 页数:9 大小:38.50KB
下载 相关 举报
【计算机类职业资格】C++语言笔试-8及答案解析.doc_第1页
第1页 / 共9页
【计算机类职业资格】C++语言笔试-8及答案解析.doc_第2页
第2页 / 共9页
【计算机类职业资格】C++语言笔试-8及答案解析.doc_第3页
第3页 / 共9页
【计算机类职业资格】C++语言笔试-8及答案解析.doc_第4页
第4页 / 共9页
【计算机类职业资格】C++语言笔试-8及答案解析.doc_第5页
第5页 / 共9页
点击查看更多>>
资源描述

1、C+语言笔试-8 及答案解析(总分:100.00,做题时间:90 分钟)一、操作题(总题数:3,分数:100.00)1.请使用 VC6 或使用答题菜单打开考生文件夹 proj1 下的工程 projl。程序中位于每个“/ERROR *found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:Name:Smith Age:21 ID:99999 CourseNum: 12Record:970注意:只修改每个“/ERROR *found*”下的那一行,不要改动程序中的其他内容。#include iostreamusing namespace std;class Studentlnfo

2、protected:/ERROR *found*char Name;int Age;int ID;int CourseNum;float Record;public:Studentlnfo(char*name,int Age,int ID,int courseNum,float record);/ERROR *found*void Studentlnfo()float AverageRecord()return Record/CourseNum;void show()constcout“Name:“Name“Age:“CourseNum:“CourseNum“Record:“Recordend

3、l;/ERROR *found*Studentlnfo Studentlnfo (char*Name,int Age,int ID,int CourseNum,float Record)Name=name;Age=age;this-ID=ID;CourseNum=courseNum;Record=record;int main()Studentlnfo st(“Smith“,21,99999,12,970);st.show();return 0;(分数:30.00)_2.请使用 VC6 或使用答题菜单打开考生文件夹 proj2 下的工程 proj2,其中定义了 vehicle 类,并派生出 m

4、otorcar 类和 bicycle 类。然后以 motorcar 和 bicycle 作为基类,再派生出 motorcycle 类。要求将vehicle 作为虚基类,避免二义性问题。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:801501001注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“/ *found*”。#include iostream.hclass vehicleprivate:int MaxSpeed;int Weight;public:/*found*vehicle (int maxspeed,intw

5、eight):_vehicle();int getMaxSpeed()return MaxSpeed;int getWeight()return Weight;/*found*class bicycle:_public vehicleprivate:int Height;public:bicycle(int maxspeed,int weight,int height):vehicle(maxspeed,weight),Height(height)int getHeight()return Height;/*found*class motorcar:_public vehicleprivate

6、:int SeatNum;public:motorcar(int maxspeed,int weight,int seatnum):vehicle(maxspeed,weight),SeatNum(seatnum)int getSeatNum()return SeatNum;/*found*class motorcycle:_public:motorcycle(int maxspeed,intweight,int height):vehicle (maxspeed,weight),bicycle(maxspeed,weight,height),motorcar(maxspeed,weight,

7、1);void main ()motorcycle a(80,150,100);couta.getMaxSpeed()endl;couta.getWeight()endl;couta.getHeight()endl;couta.getSeatNum()endl;(分数:30.00)_3.请使用 VC6 或使用答题菜单打开考生文件夹 proj3 下的工程 proj3,其中声明的 CDeepCopy 是一个用于表示矩阵的类。请编写这个类的赋值运算符成员函数 operator=,以实现深层复制。要求:补充编制的内容写在“/*333*”与“/*666*”之间。不得修改程序的其他部分。注意:程序最后将结

8、果输出到文件 out.dat 中。输出函数 writeToFile 已经编译为 obj 文件,并且在本程序中调用。/CDeepCopy.h#includeiostream#includestringusing namespace std;class CDeepCopypublic:int n; /动态数组的元素个数int*p; /动态数组首地址CDeepCopy(int);CDeepCopy();CDeepCopy/赋值运算符函数;void writeToFile (char*);/main.cpp#include“CDeepCopy.h“CDeepCopy:CDeepCopy()delete

9、p;CDeepCopy:CDeepCopy(int k)n=k;p=new intn;)/构造函数实现CDeepCopya.p0=1;d.p0=666;/对象 a,d 数组元素的赋值CDeepCopy b(3);a.p0=88;b=a;/调用赋值运算符函数coutb.p0;/显示内层局部对象的数组元素coutd.p0;/显示 d 数组元素 a.p0的值cout“d fade away;/n“;couta.p0;/显示 a 数组元素 a.p0的值writeToFile(“ “);return 0;(分数:40.00)_C+语言笔试-8 答案解析(总分:100.00,做题时间:90 分钟)一、操作

10、题(总题数:3,分数:100.00)1.请使用 VC6 或使用答题菜单打开考生文件夹 proj1 下的工程 projl。程序中位于每个“/ERROR *found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:Name:Smith Age:21 ID:99999 CourseNum: 12Record:970注意:只修改每个“/ERROR *found*”下的那一行,不要改动程序中的其他内容。#include iostreamusing namespace std;class Studentlnfoprotected:/ERROR *found*char Name;int Ag

11、e;int ID;int CourseNum;float Record;public:Studentlnfo(char*name,int Age,int ID,int courseNum,float record);/ERROR *found*void Studentlnfo()float AverageRecord()return Record/CourseNum;void show()constcout“Name:“Name“Age:“CourseNum:“CourseNum“Record:“Recordendl;/ERROR *found*Studentlnfo Studentlnfo

12、(char*Name,int Age,int ID,int CourseNum,float Record)Name=name;Age=age;this-ID=ID;CourseNum=courseNum;Record=record;int main()Studentlnfo st(“Smith“,21,99999,12,970);st.show();return 0;(分数:30.00)_正确答案:(1)char*Name;(2)Studentlnfo()(3)Studentlnfo:Studentlnfo(char*name,int age,intID,int courseNum,float

13、 record)解析:解析 本题考查 Studentlnfo 类,其中涉及构造函数、动态数组、析构函数和成员函数。声明数组时要指定数组的大小,否则将会导致程序出错,不论是构造函数还是析构函数都不能在函数名前添加返回类型。(1)主要考查考生对动态数组的掌握,由题目可知 Name 应该指向一个动态数组,而不是一个有效 char 型字符,因此要定义成 char 型指针。(2)主要考查考生对析构函数的掌握,析构函数不需要函数返回类型,应把 void 去掉。(3)主要考查考生对构造函数定义的掌握,构造函数也要使用作用域符号“:”。2.请使用 VC6 或使用答题菜单打开考生文件夹 proj2 下的工程 p

14、roj2,其中定义了 vehicle 类,并派生出 motorcar 类和 bicycle 类。然后以 motorcar 和 bicycle 作为基类,再派生出 motorcycle 类。要求将vehicle 作为虚基类,避免二义性问题。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:801501001注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“/ *found*”。#include iostream.hclass vehicleprivate:int MaxSpeed;int Weight;public:/*found

15、*vehicle (int maxspeed,intweight):_vehicle();int getMaxSpeed()return MaxSpeed;int getWeight()return Weight;/*found*class bicycle:_public vehicleprivate:int Height;public:bicycle(int maxspeed,int weight,int height):vehicle(maxspeed,weight),Height(height)int getHeight()return Height;/*found*class moto

16、rcar:_public vehicleprivate:int SeatNum;public:motorcar(int maxspeed,int weight,int seatnum):vehicle(maxspeed,weight),SeatNum(seatnum)int getSeatNum()return SeatNum;/*found*class motorcycle:_public:motorcycle(int maxspeed,intweight,int height):vehicle (maxspeed,weight),bicycle(maxspeed,weight,height

17、),motorcar(maxspeed,weight,1);void main ()motorcycle a(80,150,100);couta.getMaxSpeed()endl;couta.getWeight()endl;couta.getHeight()endl;couta.getSeatNum()endl;(分数:30.00)_正确答案:(1)MaxSpeed(maxspeed),Weight(weight);(2)virtual(3)virtual(4)public bicycle,public motorcar)解析:解析 本题考查 vehicle 类及其派生类 motorcar

18、和 bicycle,再由这两类派生出 motorcycle 类,其中涉及构造函数、成员函数和析构函数。构造函数采用成员初始化列表来完成对私有成员的初始化,派生类的书写要注意关键字。(1)主要考查考生对构造函数的掌握,构造函数使用初始化列表来对私有成员 MaxSpeed 和 Weight 初始化。(2)主要考查考生对派生类的掌握,题目要求将 vehicle 作为虚基类,避免二义性问题。因此在这里添加virtual 使 vehicle 成为虚基类。(3)主要考查考生对派生类的掌握,题目要求以 motorcar 和 bicycle 作为基类,再派生出 motorcycle 类。在主函数中可以看到 m

19、otorcycle 类的实例 a 调用 getHeight 函数和 getSeat-Num 函数,由此可知这两个基类都是公有继承,因此得出语句:public bicycle,public motorcar。虚基类继承时要添加关键字 virtual,以避免二义性。3.请使用 VC6 或使用答题菜单打开考生文件夹 proj3 下的工程 proj3,其中声明的 CDeepCopy 是一个用于表示矩阵的类。请编写这个类的赋值运算符成员函数 operator=,以实现深层复制。要求:补充编制的内容写在“/*333*”与“/*666*”之间。不得修改程序的其他部分。注意:程序最后将结果输出到文件 out.

20、dat 中。输出函数 writeToFile 已经编译为 obj 文件,并且在本程序中调用。/CDeepCopy.h#includeiostream#includestringusing namespace std;class CDeepCopypublic:int n; /动态数组的元素个数int*p; /动态数组首地址CDeepCopy(int);CDeepCopy();CDeepCopy/赋值运算符函数;void writeToFile (char*);/main.cpp#include“CDeepCopy.h“CDeepCopy:CDeepCopy()deletep;CDeepCopy

21、:CDeepCopy(int k)n=k;p=new intn;)/构造函数实现CDeepCopya.p0=1;d.p0=666;/对象 a,d 数组元素的赋值CDeepCopy b(3);a.p0=88;b=a;/调用赋值运算符函数coutb.p0;/显示内层局部对象的数组元素coutd.p0;/显示 d 数组元素 a.p0的值cout“d fade away;/n“;couta.p0;/显示 a 数组元素 a.p0的值writeToFile(“ “);return 0;(分数:40.00)_正确答案:(n=r.n; /把对象 r 字符长度赋值给 ndeletep; /删除动态数组 pp=new intn;/给动态数组 p 分配空间为 nfor(int i=0;in;i+)/遍历对象 r 中的数组 ppi=r.pi;/把 r.i赋值给 pireturn*this; /返回被赋值的对象)解析:解析 本题考查 CDeepCopy 类,其中涉及动态数组、构造函数、析构函数和运算符重载。运算符重载要先把类的定义弄清,其次要理解被重载的运算符的含义。主要考查考生对运算符重载的掌握,由注释可知此处要实现赋值运算符函数。要重载的运算符是“=”,该类的成员是动态数组 p,数组元素个数为 n,因此先释放原来的动态数组,再分配空间,然后逐个复制元素即可。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 考试资料 > 职业资格

copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
备案/许可证编号:苏ICP备17064731号-1