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

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

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

2、iclude 2 using namespace std; 3 4 enum Pets_typedog, cat, bird, fish; 5 class Pets 6 private: 7 char*name; 8 Pets_type type; 9 public: 10 Pets(const char*name=“sonny“, Pets_type type=dog); 11 Pets 12 PetS(); 13 void show()const; 14 ; 15 PetS: Pets(const char*name, Pets_type type) 16 构造函数 17 18 this-

3、name=new charstrlen(name)+1; 19 strcpy(this-name, name); 20 ERROR *found* 21 type=type; 22 23 Pets: Pets()析构函数,释放 name所指向的字符串 24 25 ERROR *found* 26 name= 0; 27 28 Pets Pets: operator=(const Pets s) 29 30 if( s=this)确保不要向自身赋值 31 return*this; 32 deletename; 33 name=new charstrlen(s name)+1; 34 ERROR

4、*found* 35 strcpy(this-name, name); 36 type=s type; 37 return*this; 38 39 void Pets: show()const 40 41 cout 2 using namespace std; 3 class Book “书 ”类 4 char*title;书名 5 int num pages; 页数 6 char*writer; 作者姓名 7 public: 8 Book(conSt char*the title, int pages, const char*the writer): num 9 pages(pages) 1

5、0 title=new charstrlen(the title)+1; 11 Strcpy(title, the title); 12 *found* 13 _ 14 Strcpy(writer, the writer); 15 16 *found* 17 Book()_ 18 int numOfPages()constreturn numpages; 返回书的页数 19 const char*theTitle()constre-turn title; 返回书名 20 const char*theWriter()const re-turn writer; 返回作者名 21 ; 22 clas

6、s TeachingMaterial: public Book 23 “教材 ”类 24 char*course; 25 public: 26 TeachingMaterial(const char *the_title, int pages, const char*the_writer, const char*the course) 27 *found* 28 : _ 29 course=new char strlen(the_course)+1; 30 strcpy(course, the course); 31 32 TeachingMaterial()deletecourse; 33

7、34 const char*theCourse()constre-turn course; ) 返回相关课程的名称 35 ; 36 int main() 37 TeachingMaterial a book(“C+语言程序设计 “, 299, “张三 “, “面向对象的程序设计 “); 38 cout 3 using namespace std; 4 5 const int Max=100; 6 Class IntSet 7 8 publiC: 9 IntSet() 10 构造一个空集合 11 12 end=-1; 13 14 IntSet(int a, int si ze)构造一个包含数组

8、a中 size个元素的集合 15 16 if(Size=Max) 17 end=Max-1; 18 else 19 end=size-1; 20 for(int i=0; itype=type; (2)deletename; (3)strcpy(this-name, s name); 【试题解析】 (1)主要考查考生对构造函数的掌握情况,因为形参名和类的私有成员名称都是 type,为了避免混淆,所以规定类的私有成员使用 this指针调用,即: this-type=type;。 (2)主要考查考生对析构函数的掌握情况,题目中要求,释放 name所指向的字符串。要释放 name指针用 delete

9、语句,即 deletename;。 (3)主要考查考生对 strcpy函数的掌握情况, strcpy函数的形参为两个字符串,而 name为指向字符串的指针,因此使用语句: strcpy(this-name, s name);。 二、简单应用题 2 【正确答案】 (1)writer=new chaistrlen(the_writer)+1; (2)deletetitle, writer; (3)Book(the_title, pages, the_writer) (4)a_book theCourse()=Max) 3 end=Max-1; 4 e1se 5 end=size-1; 6 for(int i=0; i=end; i+) 7 elementi=ai; 8 可知数组 element用来装载集合, end表示数组长度,因此调用函数IsMemberOf来判断 set中的元素是否存在于集合中,如果存在则放人数组 a中。

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

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

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