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

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

1、国家二级 C+机试(操作题)模拟试卷 335及答案与解析 一、基本操作题 1 请使用 VG6或使用【答题】菜单打开考生文件夹 pmjl下的工程 proj1,其中有枚举 DOGCOLOR、狗类 Dog和主函数 main的定义。程序中位于每个 “ERROR*found*”下的语句行有错误,请加以改正。改正后程序的输出结果应该是: There is a white dog named Hoho There is a black dog named HaIla There is a motley dog named Hihi 注意:只修改每个 “ ERROR*found*”下的那一行,不要改动程序中的

2、其他内容。 #include using namespace std; 狗的颜色:黑、白、黄、褐、花、其他 enum DOGCOLORBLACK, WHTTE, YELLOW, BROWN, PTEBALD, OTHER); class Dog /狗类 DOGCOLOR colot; char name2 O; static int count; public: Dog(char name, DOGCOLOR color) strcpy(this一 name, name); ERROR*found* strcpy(this一 color, color); DOGCOLOR getColor()

3、constreturn color; ) ERROR*found* COnSt char*get: Name()constreturn*name; ) const char* getColorString() const switch(colot) CaSe BLACK: return”blaCk”; case WHITE: return”white”; CaSe YELLOW: return”yellow”; case BROWN: return”brown”; CaSe PIEBALD: return”piebald”; return”motley”; Void show()const c

4、out using namespace std; class Member ERROR*found* private: Member(int val): value(val) int value; , class MyClass Member m; public: ERROR*found* MyClass(int val) int GetValue()constreturn m value; ) ; int main() MyClass*obj=new MyClass(i0); ERROR*found* 下列语句输出 obj指向类中的 value值 cout using namespace s

5、td; class IntStack 整数栈类 public: virtual VOid push(int)=0;入栈 virtual int pop()=0; 出栈并返回出栈元素 virtual int topElement()const=0; 返回栈顶元素。但不出栈 virtual bool isEmpty()oonst=0; 判断是否栈空 ; class SeqStack: public IntStack int data100; 存放栈元素的数组 int top; 栈顶元素的下标 public: *found* SeqStack(): _()把 top初 始化为一 1表示栈空 void

6、 push(int n)data+top= n; ) *found* int pop()return_; int topElement()constreturn data top; bool isEmpty()constreturn top=一 1; ) ; struct Node int data; Node*next; ; class LinkStack: public IntStack Node*top; public: * found* LinkStack(): _)把 top 初始化为 NULL表示栈空 void push(int n) Node*P=new Node; P-data

7、=n; * found* _; top=P; int pop() int d=top-data; top=top-next; return d; int topElement()constreturn top 一 data; ) bool isEmpty()constreturn top= NULL; ) , void pushData(IntStack DataList(DataList&data); int length()constreturn len; ) double getElement(int i)constreturn di; DataList operator+(const

8、DataList& list)const;两个数据表求和 void show()const;显示数据表 ; void writeToFile (char *, const DataList&); main cpp #include“DataList h“ DataList: DataList(int fen, double data): len(len) d=new double1en; for(int i=0; i using namespace std; clas s ValArray int*v; int Size; public: ValArray(const int*P, int n

9、): Size(n) v=new intsize; for(int i=0; icolor=color; (2)const char getName()constreturn *name; (3)Dog dogl(”Hoho”, WHITE), dog2(”Haha”, BLACK), dg3(”Hihi”,OTHER); 【试题解析】 (1)主要考查 考生对 strcpy函数的掌握,如果看到上一条语句strcpy(this一 nallle, name);,就以为本条语句也要用 strcpy函数来赋值,这是错误的。 Strcpy函数只能复制字符串,根据类的私有成员声明可知, color是DOG

10、COLOR型的,这里直接使用赋值语句 “=”即可。 (2)主要考查考生对函数返回值的掌握,先解读语句 const char*getName()constreturn*name; ,要返回的是一个 const的字符指针,同时函数内的值不能改变, name在:类的私有成员声明中是 个字符数组, *name代表字符数组而不是字符指针;问题就出来了,需要修改返回类型: constchar getName()constreturn*name; 。 (3)语法错误,定义变量时,变量之间应使用 “, ”分开。 2 【正确答案】 (1)public: (2)MyClass(int val): _m(val)或

11、 MyClass(int val)_m=val (3)coutGetValue()”。 二、简单应用题 3 【正确答 案】 (1)top(一 1) (2)datatop- (3)top(NULL) (4)p-next=top 【试题解析】 (1)主要考查考生对构造函数的掌握情况,先看语句注释:把 top初始化为一 1表示栈空,即要把 top赋值为一 1即可。 (2)主要考查考生对纯虚函数的掌握情况,先看纯虚函数在基类的注释:出栈并返回出栈元素。要返回栈顶元素可以通过 datatop得到,出栈同时要使得 top往下移动,即 top-。 (3)主要考查考生对构造函数的掌握情况,先看语句注释:把 t

12、op初始化为NULL表示栈空,因此使用成员列表初始化直接把 top赋值为 NULL即可。 (4)主要考查考生对栈的掌握, push为入栈函数, top指向栈顶元素,因此新添加的指针的 next要指向 top,即 p一 next=top:。 三、综合应用题 4 【正确答案】 for(int i=0; i len; +i) 遍历对象 list中的数组和 d数组,把对应的值相加后放到数组 dd中。 ddi=di+list di; 【试题解析】 主要考查考生对重载运算符的掌握,题目要求对两个数据表 求和。程序已经定义了动态数组 dd,并已经分配好了空间,接下来只要运用循环语句完成元素相加并进行赋值即可

13、。 5 【正确答案】 size=other size; 把对象数组的大小赋值给 size V=Dew intother size ; 根据对象数组的大小动态分配数组 v for(int i=0; isize; +i) vi=other vi; 遍历整个对象的数组把值 other vi放到数组 v中 【试题解析】 主要考查考生对复制构造函数的掌握。由函数名: ValArray: :ValArray(const。 ValArray&other),知道要复制的对象是 other,对由 ValArray类的成员: int* v; int size;知道要复制的内容是动态数组 v及整型变量 size。动态数组要使用 new语句分配内存,最后利用 for循环语句来完成复制过程。

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

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

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