[计算机类试卷]国家二级C++机试(操作题)模拟试卷503及答案与解析.doc

上传人:amazingpat195 文档编号:497603 上传时间:2018-11-28 格式:DOC 页数:8 大小:38KB
下载 相关 举报
[计算机类试卷]国家二级C++机试(操作题)模拟试卷503及答案与解析.doc_第1页
第1页 / 共8页
[计算机类试卷]国家二级C++机试(操作题)模拟试卷503及答案与解析.doc_第2页
第2页 / 共8页
[计算机类试卷]国家二级C++机试(操作题)模拟试卷503及答案与解析.doc_第3页
第3页 / 共8页
[计算机类试卷]国家二级C++机试(操作题)模拟试卷503及答案与解析.doc_第4页
第4页 / 共8页
[计算机类试卷]国家二级C++机试(操作题)模拟试卷503及答案与解析.doc_第5页
第5页 / 共8页
点击查看更多>>
资源描述

1、国家二级 C+机试(操作题)模拟试卷 503及答案与解析 一、基本操作题 1 请打开考生文件夹下的解决方案文件 proj1,此工程中包含了类 Pets(“宠物 ”)和主函数 main的定义。程序中位于每个 “ ERROR*found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: Name: sonny Type: dog Name: John Type: dog Name: Danny Type: cat Name: John Type: dog 注意:只修改每个 “ ERROR*found*”下的那一行,不要改动程序中的其他内容。 #include iostream usi

2、ng namespace std; enum Pets typedog, cat, bird, fish; class Pets private: char*name; Pets_typetype; public: Pets, const char*name=“sonny“, Pets type type=dog); Pets&operator=(const Pets &s); Pets( ); void show( )conSt; ; Pets: Pets(const char*name, Pets_type type) 构造函数 this- name=new charstrlen(name

3、)+1; strcpy(this- name, name); ERROR*found* type=type; Pets: Pets( )析构函数,释放 name所指向的字符串 ERROR*found* name= 0; Pets&Pets: operator=(constPets&S) if(&s=this)确保不要向自身赋值 return*this; delete name; name=new charstrlen(s name)+1; ERROR*found* strcpy(this- name, name); type=s type; return*this; void Pets: sh

4、ow( )const cout “Name: “ name “Type: “; switch(type) case dog: cout “dog“; break; casecat: cout “cat“; break; case bird: cout “bird“; break; case fish: cout “fish“; break; cout endl; int main( ) Pets mypet1, mypet2(“John“, dog); Petsyoupet(“Danny“, cat); mypet1 show( ); mypet2 show( ); youpet show(

5、); youpet: mypet2; youpet show( ); return0; 二、简单应用题 2 请打开考生文件夹下的解决方案文件 proj2,此工程中包含一个程序文件main cpp,其中有 “班级 ”类 Class和 “学生 ”类 Student的定义,还有主函数 main的定义。在主函数中定义了两个 “学生 ”对象,他们属于同一班级。程序展示,当该班级换教室后,这两个人的教室也同时得到改变。请在横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为: 改换教室前: 学号: 0789 姓名: 张三 班级: 062113 教室: 521 学号: 0513 姓

6、名:李四 班级: 062113 教室: 521 改换教室后: 学号: 0789 姓名:张三 班级: 062113 教室: 311 学号: 0513 姓名:李四 班级: 062113 教室: 311 注意:只能在横线处填写适当的代码,不要改动 程序中的其他内容。 #include iostream using namespace std; classClasS “班级 ”类 public: ClasS(const char*id, constchar*room) strcpy(class_id, id); *found* _ const char*getClassID( )constreturn

7、 class_id; 返回班号 *found* const char*getClasSroom( )const_返回所在教室房号 void changeRoomTo(const char*new_room)改换到另一个指定房号的教室 strcpy(classroom, new_room); private: char class_id20;班号 char classroom20;所在教室房号 , class Student “学生 ”类 char my_id10;学号 char my_name20;姓名 Class&my_class;所在教室 public: *found* Student(c

8、onst char*the_id, const char*the_name, Class&the_class): _ strcpy(my_id, the_id); strcpy(my_name, the_name); const char*getID( )constreturn my_id; const char*getName( )constreturn my_name; Class getClass( )constreturn my_class; ; void showStudent(Student*stu) cout “学号: “ stu- getID( ) “ “; cout “姓名:

9、 “ stu- getName( ) “ “; cout “班级: “ stu- getClass( ) getClasSID( ) “ “; cout “教室: “ stu- getClass( ) getClassroom( ) endl; int main( ) Class cla(“062113“, “521“); Student Zhang(“0789“, “张三 “, cla), Li(“0513“, “李四 “, cla); cout “改换教室前: “ endl; showStudent(&Zhang); showStudent(&Li); 062113班的教室由 521改换到

10、 311 *found* cout “改换教室后: “ endl; showStudent(&Zhang); showStudent(&Li); return0; 三、综合应用题 3 请打开考生文件夹下的解决方案文件 proj3,此工程中包含一个源程序文件proj3 cpp,其中定义了用于表示平面坐标系中的点的类 MyPoint和表示矩形的类MyRectangle;程序应当显示: (0, 2)(2, 2)(2, 0)(0, 0)4 但程序中有缺失部分,请按照以下提示,把缺失部分补充完整: (1)在 “ *1* *found*”的下方是构造函数的定义,它用参数提供的左上角和右下角的坐标对 up_

11、left和 down_right进行初始化。 (2)在 “ *2* *found*”的下方是成员函数 getDownLeft的定义中的一条语句。函数 getDownLeft返回用 MyPoint对象表示的矩形的左下角。 (3)在 “ *3* *found*”的下方是成员函数 area的定义,它返回矩形的面积。 注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动 “*found*”。 proj3 cpp #include iostream using namespace std; class MyPoint表示平面坐标系中的点的 类 double x; double y;

12、 public: MyPoint(double x, doubley )this- x=x; this- y=y; double getX( )constreturn x; double getY( )constreturn y; void show( )constcout ( x , y ); ; class MyRectangle表示矩形的类 MyPoint up_left;矩形的左上角顶点 MyPoint down_right;矩形的右下角顶点 public: MyRectangle(MyPoint upleft, MyPoint downright); MyPoint getUpLef

13、t( )constreturn up_left; )返回左上角坐标 MyPoint getDownRight( )constreturn down_right; 返回右下角坐标 MyPoint getUpRight( )const; 返回右上角坐标 MyPoint getD0wnLeft( )const; 返回左下 角坐标 double area( )const;返回矩形的面积 ; *1*found* MyRectangle: MyRectangle (_): up_left(p1), down_right (p2) MyPoint MyRectangle: getUpRight ( )con

14、st return MyPoint(down right getX( ), up_left getY( ); MyPoint MyRectangle: getDownLeft( )const *2* *found* return MyPoint(_); ) *3* *found* _area( )conSt return(getUpLeft( ) getX( )getDownRight( ) getX( )*(getDownRight( ) getY( )-getU-pLeft( ) getY( ); ) int msin( ) MyRectangle r(MyPoint(0, 2), MyP

15、oint(2, 0); r getUpLeft( ) show( ); r getUpRight( ) show( ); r getDownRight( ) show( ); r getDownLeft( ) show( ); cout r area( ) endl; return0; 国家二级 C+机试(操作题)模拟试卷 503答案与解析 一、基本操作题 1 【正确答案】 (1)this- type=type; (2)delete name; (3)strcpy(this- name, s name); 【试题解析】 (1)主要考查考生对构造函数的掌握情况,因为形参名和类的私有成员名称都是

16、type,为了避免混淆,所以规定类的私有成员使用 this指针调用,即: this- type=type;。 (2)主要考查考生对析构函数的掌握情况,题目中要求,释放 name所指向的字符串。要释放 name指针用 delete语句,即 delete name;。 (3)主要考查考生对 strcpy函数的掌握情况, Strcpy函数的形参为两个字符串,而naille为指向字符串的指针,因此使用 语句: strcpy(this_ name, s name);。 二、简单应用题 2 【正确答案】 (1)strcpy(classroom, room); (2)return classroom; (3

17、)my_class(the_class) (4)cla changeRoomTo(“311“); 【试题解析】 (1)主要考查考生对 strcpy函数的掌握情况,根据上一条语句:strcpy(class_id, id);可知,这条语句要复制字符串 room,因此使用 strcpy函数复制,即 strcpyr(classroom, room);。 (2)主要考查考生对函数返回值的掌握情况,根据函数要求:返回所在教室房号及函数要求返回的类型为 const char*,可以得出这里直接使用 return语句返回classroom即可。 (3)主要考查考生对构造函数的掌握情况,先看函数体中: stre

18、py(my_id, the_id); strcpy(my_name, the_name); 可知只有参数 Class&the_class未使用,因此在这里使用成员列表初始化给my_class赋初始值。 (4)主要考查考生对成员函数调用的掌 握,程序要求 062113班的教室由 521改换到311。在类 Class中已经定义了函数: void changeRoomTo(eonstchar*newroom),因此直接调用函数 changeRoomTo即可。 三、综合应用题 3 【正确答案】 (1)MyPotnt p1, MyPotnt p2 (2)up_left getx( ), down_r4g

19、ht getY( ) (3)double MyRectangle: 【试题解析】 (1)考查构造函数,构造函数中的参数要给私有成员赋值,在下句中 up_left(p1), down_right(p2)指出私有成员赋值要使用形参 p1和 p2,因此这里参数要定义为 MyPoint p1, MyPoirIt p2。 (2)主要考查成员函数的返回语句, MyPoint My Rectangle: getDownLeft( )const函数要求返回一个左下角的点坐标,因此使用语句 MyPoint(up_left getX( ),down_right getY( );。 (3)主要考查成员函数的定义,在 MyRectangle类中已经声明 double area( )const,因此此处只要添加 double MyRectangle:即可。

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

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

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