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

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

1、国家二级 C+机试(操作题)模拟试卷 278及答案与解析 一、基本操作题 1 请使用 VC6或使用【答题】菜单打开考生文件夹 proj1下的工程 proj1,此工程中含有一个源程序文件 proj1 epp。其中位于每个注释 “ERROR*found*”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为: Constructor called The value is 10 Max number is 20 Destructor called 注意 :只能修改注释 “ ERROR*found*”的下一行语句,不要改动程序中的其他内容。 proj1 cpp #include iostrea

2、m US ing namespace std; class MyClass public: ERROR*found* void MyClasS(int i) value i; cout ”Constructor called ” end1; int Max(int X, int Y)return X y?X: y; )求两个整数的最大值 ERROR*found* int Max(int x, int Y, int z 0) 求三个整数的最大值 if(x y) return x z?X: z; else return Y z?Y: z; int GetValue()constreturn val

3、ue; MyClass()cout “Destructor called “ end1; private: int value; ; int main() MyClasS obj(10); ERROR*found* cout “The value is“ value() end1; cout “Max number is“ obj Max(10, 20) end1; return 0; 二、简单应用题 2 请使用 VC6或使用【答题】菜单打开考生文件夹 proj2下的工程 proj2,该工程中含有一个源程序文 件 proj2 epp,请将堆栈类的定义补充完整。使程序的输出结果为: The el

4、ement of stack are: 4 3 2 1 注意:请勿修改主函数 main和其他函数中的任何内容,只在横线处编写适当代码,不要改动程序中的其他内容,也不要删除或移动 “ *found*”。 proj2 cpp #include ioStream USing namespace std; const int Size 5; class Stack; Class Item public: *found* Item(const int val): _ 构造函数对 item进行初始化 private: int item; Item * next; friend clasS Stack; ;

5、 class Stack public: Stack(): top(NULL) Stack(); int Pop(); void Push(const int ); private: Item * top; ; Stack Stack() Item * P top, *q; while(P! NULL) q P next; *found* _;释放 P所指向的 节点 p q; int Stack Pop() Item * temp; int ret; *found* _;使 temp指向栈顶节点 ret top item; top: top next; delete temp; return

6、ret; void Stack Push(const int&val) Item* temp new Item(val); *found*_; 使新节点的 next指针指向栈顶数据 top temp; int main() Stack s; for(int i 1; i Size; i ) s Push(i); cout “The element of stack are: “; for(i 1; i Size; i ) cout s Pop() t; return 0; 三、综合应用题 3 请使用 VC6或使用【答题】菜单打开考生文件夹 proj3下的工程文件 proj3,此工程中包含一个源

7、程序文件 proj3 epp,其中定义了用于表示平面坐标系中的点的类 MyPoint和表示矩形的类 MyRectangle;程序应当显示: (0, 2)(2, 2)(2, 0)(0, 0)4 但程序中有缺失部分,请按照以下提示, 把缺失部分补充完整: (1)在 “ *1* *found*”的下方是构造函数的定义,它用参数提供的左上角和右下角的坐标对 up_left和 down_right进行初始化。 (2)在 “ *2* *found*”的下方是成员函数 getDownLeft的定义中的一条语句。函数 getDownLeft返回用 MyPoint对象表示的矩形的左下角。 (3)在 “ *3*

8、*found*”的下方是成员函数 area的定义,它返回矩形的面积。 注意:只 在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动 “*found*”。 proj3 cpp #include iostream using namespace std; class MyPoint表示平面坐标系中的点的类 double x; double y; public: MyPoint(double X, double y) this x x; this y y; ) double getX()constreturn x; ) double getY()constreturn y; ) vo

9、id show()constcout ( X , Y ); ) ; class MyRectangle 示矩形的类 MyPoint up_left; 矩形的左上角顶点 MyPoint down_right;矩彤的右下角顶点 public: MyRectangle(MyPoint upleft, MyPoint downright); MyPoint getUpLeft()constreturn up_left; 返回左上角坐标 MyPoint getDownRight()const return downright; 返回右下角坐标 MyPoint getUpRight()const; 返回右

10、上角坐标 MyPoint getDownLeft()const; 返回左下角坐标 double area()const; 返回矩形的面积 ; *1* *found* MyRectangle MyRectangle (_): up_left(p1), down_right(p2) MyPoint MyRectangle getUpRight ()const return MyPoint(down_right getX(), up left getY(); MyPoint MyRectangle getDown Left()const *2* *found* return MyPoint(_);

11、*3* *found* _area()const return(getUpLeft() getX() getDownRight() getX() * (getDownRight() getY() getUpLeft() getY(); int main() MyRectangle r(MyPoint(0, 2), MyPoint(2, 0); r getUpLeft() show(); r getUpRight() show(); r getDownRight() show(); r getDownLeft() show(); cout r area() end1; return 0; 国家二

12、级 C+机试(操作题)模拟试卷 278答案与解析 一、基本操作题 1 【正确答案】 (1)MyClass(int i) (2)int Max(int x, int y, int z) (3)cout “The value is“ obj GetValue() end1: 【试题解析】 (1)考查构造函数,构造函数前不加 void或其他任何类型名,直接使用 MyClass(int i)即可。 (2)主要考查函数重载,在 int: Max(int x, int y)return x y?x: y; 中两个形参变量都是 int型,而语句int Max(int x, inty, int z 0)的前两

13、个形参也都是 int型,第三个形参定义默认值,那么这两个 Max函数在调用时它们的参数个数和参数类型都一样,因为函数重载要求形参类型或形参个数不同,所以要把 int z 0改为 int z,才能构成函数重载 。 (3)主要考查成员函数的调用,因为 value是私有成员,所以不能被类外函数直接调用,而且 value()的用法也是错误的,可以使用成员函数 obj GetValue()得到value的值。 二、简单应用题 2 【正确答案】 (1)item(val) (2)deletep (3)temp top (4)temp next top 【试题解析】 (1)主要考查构造函数,对私有成员进行初始

14、化,即 item(val)。 (2)主要考查使用 delete语句释放指 针,一般格式为: delete指针。 (3)指向栈顶节点的是 top指针,要使 temp指向栈顶节点,故使用语句temp=top;。 (4)指向栈顶节点的是 top指针,要使新节点的 next指针指向栈顶数据,故使用语句 temp next top: o 三、综合应用题 3 【正确答案】 (1)MyPoint p1, MyPoint p2 (2)up_left getX(), down_right getY() (3)double MyRectangle 【试题解析】 (1)考查构造函数,构造函数中的参数要给私有成员赋值,在下句中 up_left(p1), down_right(p2)指出私有成员赋值要使用形参 p1和 p2,因此这里参数要定义为 MyPoint p1, MyPoint p2。 (2)主要考查成员函数的返回语句, MyPoint MyRectangle getDownLeft()const函数要求返回一个左下角的点坐标,因此使用语句 MyPoint(up_left getX(),down_right getY();。 (3)主要考查成员函数的定义,在 MyRectangle类中已经声明 double area()const,因此此处只要添加 double MyRectangle 即可。

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

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

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