[计算机类试卷]国家二级C++机试(操作题)模拟试卷503及答案与解析.doc
《[计算机类试卷]国家二级C++机试(操作题)模拟试卷503及答案与解析.doc》由会员分享,可在线阅读,更多相关《[计算机类试卷]国家二级C++机试(操作题)模拟试卷503及答案与解析.doc(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改换到
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 计算机 试卷 国家 二级 机试 操作 模拟 503 答案 解析 DOC
