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

上传人:eastlab115 文档编号:497297 上传时间:2018-11-28 格式:DOC 页数:5 大小:30.50KB
下载 相关 举报
[计算机类试卷]国家二级C++机试(操作题)模拟试卷228及答案与解析.doc_第1页
第1页 / 共5页
[计算机类试卷]国家二级C++机试(操作题)模拟试卷228及答案与解析.doc_第2页
第2页 / 共5页
[计算机类试卷]国家二级C++机试(操作题)模拟试卷228及答案与解析.doc_第3页
第3页 / 共5页
[计算机类试卷]国家二级C++机试(操作题)模拟试卷228及答案与解析.doc_第4页
第4页 / 共5页
[计算机类试卷]国家二级C++机试(操作题)模拟试卷228及答案与解析.doc_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

1、国家二级 C+机试(操作题)模拟试卷 228及答案与解析 一、基本操作题 1 使用 VC6打开考生文件夹下的源程序文件 modi1 cpp,该程序运行时有错误,请改正程序中的错误。 本题的功能是:从键盘输入字符串 S,然后输出字符串 s,中的字符个数。 注意:不要改动 main函数,不能增行或删行,也不能更改程序的结构,错误的语句在 *error*的下面。 #include iostream int main() *error* cout “please input a string: “ endl; *error* namespace std; char s256; cin getline(

2、s, 256); cout strlen(S) endl; return0; 二、简单应用题 2 使用 VC6打开考生文件夹下的源程序文件 modi2 cpp。请完成函数fun(char*data),此函数的功能是,找出字符串数组中最小的 ASCII值,如果有相同变量,则输出最后一个所在的位置;如果字符串为空,则返回 -1;或者不存在时也返回 -1。 注意:请勿改动主函数 main与其他函数中的任何内容,仅在函数 fun的花括号中填入所编写的若干语句。计算数字如果第一个字母最小,则返回 0。依次增加。 #include iostream h int fun(char*data) void ma

3、in() char str1024; cout “请输入一行英文字符串: n“; cin getline(str, 1024); cout “最小的字母出现在距离头部 “ fun(str) “个字母处 “ endl; 三、综合应用题 3 使用 VC6打开考生文件夹下的源程序文件 modi3 cpp。请完成以下部分,实现在屏幕上输出为: TestClass3 TestClass2 这个程序需要修改的部分,请按照以下部分实现。 (1)类 TestClass0不能被实例化,请定义一个纯虚函数 print,在注释*1*后添加适当的语句。 (2)类 TestClass1私有虚继承类 TestClass0

4、,请在注释 *2*后添加适当的语句。 (3)类 TestClass2公有 继承类 TestClass0,请在注释 *3*后添加适当的语句。 (4)类 TestClass3公有继承类 TestClass2与 TestClass1,请在注释*4*后添加适当的语句。 注意:仅在函数指定位置添加语句,请勿改动主函数 main与其他函数中的任何内容。 #include ioStream h C1ass TestClass0 *1* ; *2* class TestClass1: public: void print() cout “TestClass1“ endl; ; *3* class TestCl

5、ass2: public: void print() cout “TestClass2“ endl; ; *4* Class TestClass3: public: void print() cout “TestClass3“ endl; ; void main() TestClass3 c3; TestClass2 c2; c3 print(); c2 print(); return; 国家二级 C+机试(操作题)模拟试卷 228答案与解析 一、基本操作题 1 【正确答案】 (1)std: cout “please input a string: “ std: endl; (2)using

6、namespace std; 【试题解析】 (1)第 1个标识下实现在屏幕上输出 “please input a string: ”提示语句。在程序中用到 C+标准库时,要使用 std标准命名空间进行限定。 cout为标准库函数,所以要声明 cout是在命名空间 std中定义的流对象,即第 1个标识下的 “std: cout “please input a string: “ std: endl; ”。 (2)第 2个标识下的 “namespace std; ”语句是使用了标准命名空间 std,正确语句应该是 “using namespace std; ”。 二、简单应用题 2 【正确答案】

7、int MinPos=0;初始化最小值位置 if(data=NULL)判断输入字符串是否为空 return-1; char MinData=data0;设置字符串第一个字符为最小值 if(MinData=0) 判断第一个字符是否存在 return-1; for(int i=1; datai!=0; i+) if(datai =MinData) MinData=datai;逐个判断每个字母是否小于标记字符 MinPos=i; 最小值位置 return MinPos; 【试题解析】 (1)MinPos变量首先指向字符串 data的第一个字符,同时将标志位MinPos初始值赋为 0。 (2)然后利用

8、 for循环对每一个当前字符与 MinPos标记的字符进行比较。 (3)循环中将求得的最小数据 datai赋值给 MinData,并将该数据项数组标志位赋给标志位 MinPos。 三、综合应用题 3 【正确答案】 (1)添加语句: virtual void print()=0; (2)将 “class TestClass1: ”补 充完整为: class TestClass1: virtual private TestClass0 (3)将 “class TestClass2: ”补充完整为: class TestClass2: public TestClass0 (4)将 “class Te

9、stClass3: ”补充完整为: class TestClass3: public TestClass2, public TestClass1 【试题解析】 (1)题目 1要求 “请定义一个纯虚函数 print”。在 C+中,虚函数在基类中用 virtual声明成员函 数为虚函数。纯虚函数是在声明虚函数时被 “初始化 ”为 0的函数,即 “virtual void print()=0; ”。 (12)题目 2要求 “类 TestClass1私有虚继承类 TestClass0”。在 C+中声明一个派生类时将基类的继承方式指定为 private的,称为私有继承。同 (1),声明 TestClas

10、s1虚继承类 “: virtual TestClass0”,题目要求为私有,即 “class TestClass1: virtual private TestClass0”。 (3)题目 3要求 “类 TestClass2公有继承类 TestClass0。 ”同 (2),公有继承是在定义一个派生类时将基类的继承方式指定为 public的,称为公用继承。所以这里补全“class TestClass2: public TestClass0”。 (4)题目 4要求 “类 TestClass3公有继承类 TestClass2与 TestClass1。 ”同 (3),所以这里补全 “class TestClass3: public TestClass2, public TestClass1”。

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

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

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