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

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

1、国家二级 C+机试(操作题)模拟试卷 338及答案与解析 一、基本操作题 1 请使用 VC6或使用【答题】菜单打开考生文件夹 pmjl下的工程 pmjl。其中有线段类 Line的定义。程序中位于每个 “ ERROR*found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是: End point 1=(1, 8), End point 2=(5, 2), length=7 2111。 注意:只修改每个 “ ERROR*found*”下的那一行,不要改动程序中的其他内容。 #include #include using namespace std; class Line; dou

2、ble length(Line); class Line 线段类 double x1, y1;线段端点 1 double x2, y2;线段端点 2 public: ERROR*found* Line(double x1, double yl, double x2, double y2)const this-x1=x1; this-y1=y1; this-x2=x2; this-y2=y2; double getX1()constreturn x1; double getYl()constreturn y1; double getX2()constreturn x2; double getY2

3、()constreturn y2; void show()const tout double length(Point pl, Point p2) return sqrt(p1 getX()一 p2 getX()* (p1 getX()一 p2 getX()+(p1 getY()一 p2 getY()*(p1 getY()一 p2 getY(); double Triangle: perimeter()const 一个 return语句,它利用 length函数计算并返回三角形的周长 *found* _; double Triangle: area()const double s=perime

4、ter() 2 0; return sqrt(s*(slength(pointl, point2)* (slength(point2, point3)* (Slength(point3, pointl); proj2 cpp #include”shape h” #include using namespace std; *found* show函数的函数头 (函数体以前的部分 ) cout using namespace std; class Stack public: virtual void push(char c)=0; virtual char pop()=0; ; class Arr

5、ayStack: public Stack char*P; int maxSize; int top; public: ArrayStack(int s) top=0; maxSize=s; /*found* p=_; 一 ArrayStack() /*found* _; void push(char c) if(top=maxSize) cerr us ing namespace std; class ValArray int*v; int Size; public: ValArray(const int*P, int n): size(n) v=new intsize; for(int i

6、=0; isize; i+) vi=Pi; ValArray(const ValArray void writeTOFile(const char*); main cpp #include”ValArray h” ValArray: ValArray(const ValArray v2 print(cout); coutendl; writeToFile(”); return 0; 国家二级 C+机试(操作题)模拟试卷 338答案与解析 一、基本操作题 1 【正确答案】 (1)Line(double x1, double y1, double x2, double y2) (2)cout”),

7、 length=”length(*this)“。 ” endl: (3)return sqrt(1 getXl()一 1 get)(2()*(1 getXl ()一 1 getX2()+(1 getYl()一 1 getY2()*(1 getYl ()一 1 getY2(); 【试 题解析】 (1)主要考查考生对构造函数的掌握,构造函数要给私有成员赋初始值,因此不能使用 eonst来限制。 (2)主要考查考生对 this指针的掌握,由函数 length的声明 double length(Line);可知, length函数的形参是 Line类,在 void show()const函数里,thi

8、s指针指向的是当前 Line类,因此可以用 *this表示当前 Line类。 (3)主要考查考生对成员函数的掌握, length函数是类外函数,不能直接调用类的私有成员,因此要通过成员函数取得对应的值。 二、简单应用题 2 【正确答案】 (1)X(x0), Y(y0) (2)Point point1, point2, point3 (3)retum length(point1, point2)+length(point1, point3)+length(point2, point3) (4)void show(Shape 【试题解析】 主要考查考生对复制构造函数的掌握, ValArray类的复制构造函数应实现对象的深层复制。由 ValArray类的构造函数: ValArray(const int* p, int n) : size (n) v=new intsize; 给 v分配大小为 size的空间 for (int i =0; i size; i+) /遍历 p vi=pi; 把 pi赋值给 vi 可知类中 v是动态数组, size表示数组长度,因此要先给 v分配空间为 size,再逐个元素复制以达到对象的深层复制。

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

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

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