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

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

1、国家二级 C+机试(操作题)模拟试卷 333及答案与解析 一、基本操作题 1 请使用 VC6或使用【答题】菜单打开考生文件夹 proj1下的工程 projl。程序中位于每个 “ ERROR*found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: value=63 number=1 注意:只修改每个 “ ERROR*found*”下的那一行,不要改动程序中的其他内容。 #include using namespace std; class MyClasS int*P; const int N; public: ERROR*found* MyClass(int val): N=

2、1 P=new int; *P=val; ERROR*found* 一 MyClass()delete*P; ) friend void print(MyClass obj); ; ERROR* found* void MyClass: print(MyClass obj) cout using namespace std; claSS Book “书 ”类 char*title; /书名 int num_pages;页数 char*writer; 作者姓名 public: Book(const char*the title, int pages, const char*thewriter):

3、 num_ pages(pages) title=new charstrlen(the title)+1; strcpy(title, the_title); *found* strcpy(writer, the_writer); /*found* 一 Book()_ int numOfPages()constreturn num paqes; ) 返回书的页数 const char*theTitle()constre- turn title; ) 返回书名 const char*theWriter()constre- turn writer; 返回作者名 , class TeachingMa

4、terial: public Book “教材 ”类 char*course; public: TeachingMaterial(const char*the title, int pages, const char*the writer, const char*the course) *found* : _ course=new charstrlen(the course)+1; strcpy(course, the course); TeachingMaterial()deletecourse; const char*theCourse()constre- turn course;) 返回

5、相关课程的名称 ; int main() TeachingMaterial a book(”C+语言程序 设计 ”, 299, “张三 ”, ”面向对象的程序设计 ”); cout using namespace std; class DataList数据表类 int len; double*d; public: DataList(int len, double data= NULL); 一 DataList()deleted; ) int length()constreturn len; ) 数据表长度 (即数据元素的个数 ) double getElement(int i)constre-

6、 turn di; void sort();数据表排序 void show()const;显示数据表 ; void writeToFile (char *, const DataList ); main cpp #include”DataList h DataList: DataList(int len, double data): len(len) d=new double1en; for(int i=0; i #include #include #include”intArray h” using namespace std; intArray: intArray() length=10;

7、 array=new intlength; intArray: intArray(char*filename) ifstream myFile(filename); array=new int300; length=0; while(myFilearray1ength+) length一一; myFile close(); intArray& intArray: operator = (const intArray src) if(array!=NULL)deletearray; length=src length; array=new intlength; *333* *666* retur

8、n*thiS; intArray:一 intArray() deletearray; void intArray: show() int step=0; for(int i=0; inext=top 【试题解析】 (1)主要考查构造函数,对私有成员进行初始化,即 item(val)。 (2)主要考查使用 delete语句释放指针,一般格式为: delete+指针。 (3)指向栈顶节点的是 top指针,要使 temp指向栈顶节点,故使用语句temp=top;。 (4)指向栈顶节点的是 top指针,要使新节点的 next指针指向栈顶数据,故使用语句 temp一 next=top;。 三、综合应用题

9、 4 【正确答案】 for(int 1 =0; 1 dj) di和 dj比较人 ,如果大于,就 di和 dj值交换 int temp=di; 把临时整型变量 temp赋值为 di di =dj; 把 dj赋值给 di dj=temp; 把 temp值赋给 dj 【试题解析】 本题使用最简单的冒泡排序算法,首先明确要排 序的动态数组 d,其长度为 len,在此可以使用两个下标 i和 j相比较,当 didj时,数组内的值利用中间变量 temp进行交换。 5 【正确答案】 for(int i=0; ilength; i+) 遍历对象 src中的数组 array,然后依次把值放进数组 array中 arrayi =src arrayi; 【试题解析】 主要考查考生对运算符重载的掌握,该函数要重载运算符 “=”,该运算符的意思是赋值。先看该函数的其他语句: if(array!=NULL)deletearray; length=src 1ength; array=new int1ength; 第一条语句是把原来动态数组释放,第二条语句是把形参 SYC的成员 length赋值给变量 length,第三条语句是给指针 array分配内存。接下来要把动态数组中的值逐个赋给 array数组,在这里使用 for循环语句,循环变量 i的范围是 0一length,并进行赋值操作。

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

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

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