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

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

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

2、句,不要改动程序中的其他内容。 proj1 cpp #include iostream using namespace std; class MyClass public: ERROR *found* MyClass(int i) value i; cout “Constructor called “ end1; ERROR *found* MyClass(const MyClass P) value P value; cout “Copy constructor called “ end1; void Print() cout “The value is“ value end1; ) ERR

3、OR *found* void MyClass() cout “Destructor called “ end1; private: int value; , int main() MyClass obj1; obj 1 Print(); MyClasS obj2(obj1); obj 2 Print(); return 0; 二、简单应用题 2 请使用 VC6或使用【答题】菜单打开考生文件夹 proj2下的工程 proj2,其中有矩阵基类 MatrixBase、矩阵类 Matrix和单位阵 UnitMatrix的定 义,还有 main函数的定义。请在横线处填写适当的代码并删除横线,以实现上述

4、类定义。此程序的正确输出结果应为: 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动 “ *found*”。 #include iostream using namespace std; 矩阵基础类,一个抽象类 class MatrixBase int rows r cols; public: MatrixBase(int rows, int cols): rows

5、(rows), cols(cols) int getRows()constreturn rows; 矩阵行数 int getCols()constreturn cols; 矩阵列数 virtual double getElement(int r, int c)const 0;取第 i个元素的值 void show()const 分行显示矩阵中所有元素 for(int i 0; i rows; i ) cout end1; for(int j 0; j cols; j ) *found* cout _ “; ; 矩阵类 class Matrix: public Matrix- Base doub

6、le *val; public: *found* Matrix(int rows, int cols, double m NULL): _ *found* val _; for(int i 0; i rows *cols; i ) vali (m NULL?0 0: mi); Matrix()deleteval; double getElement(int r, int c)constreturn valr*getCols() c; ) ; 单位阵 (主对角线元素都是 1,其余元素都是 0的方阵 )类 class UnitMatrix: public MatrixBase( public: U

7、nitMatrix(int rows): MatrixBase(rows, rows) 单位阵行数列数相同 double getElement(int r, int c)const *found* if(_)return 1 0; return 0 0; ; int main()f MatrixBase *m; double d5 1, 2, 3, 4, 5, 2, 3, 4, 5, 6, 3, 4, 5, 6, 7; m new Matrix(3, 5, (double*)d); m show(); delete m; cout end1; m new UnitMatrix(6); m sh

8、ow(); delete m; return 0; 三、综合应用题 3 请使用 VC6或使用【答题】菜单打开考生文件夹 proj3下的工程 proj3,其中声明的 DataList类,是一个用于表示数据表的类。 DataList的重载运算符函数operator,其功能是求当前数据表与另一个相同长度的数据表之和;即它返回一个数据表,其每个元素等于相应两个数据表对应元素之和。请编写这个 operator函数。程序的正确输出应该是: 两个数据表: 1, 2, 3, 4, 5, 6 3, 4, 5, 6, 7, 8 两个数据表之和: 4, 6, 8, 10, 12, 14 要求: 补充编制的内容写在

9、“ *333*”与 “ *666*”之间,不得修改程序的其他部分。 注意:程序最后将结果输出到文件 out dat中。输出函数 writeToFile已经编译为 obj文件,并且在本程序中调用。 DataList h #include iostream using namespace std; class DataList数据表类 int len; double *d; public: DataList(int len, double data NULL); DataList(DataList&data); int length()constreturnlen; double getEleme

10、nt(int i) constreturn di; ) DataList operator (const DataList&list)const;两个数据表求和 void show()const;显示数据表 ; void writeToFile(char *, const DataList&); main cpp #include“DataList h“ DataList DataList(int len, double data): len(1en) d new doublelen; for(int i 0; i len; i ) di (data NULL?0 0: datai); Dat

11、aList DataList(DataList &data): fen(data fen) d new doublelen; for(int i 0; i len; i ) di data di; DataList DataList operator (const DataList&list)const 两个数据表求和 double * dd new double list 1ength(); *333* *666* return DataList(list length(), dd); void DataList show()const显示数据表 for(int i 0; i len 1;

12、i ) cout di “, “; cout dlen 1 end1; int main() double sl (1, 2, 3, 4, 5, 6; double s2 3, 4, 5, 6, 7, 8); DataList listl(6, s1), list2 (6, s2);定义两个数据表对象 cout “两个数据表: “ end1; list1 show(); list2 show(); cout end1 “两个数据表之和: “ end1; (list1 list2) show(); writeToFile ( “, listl list2); return 0; 国家二级 C+机

13、试(操作题)模拟试卷 287答案与解析 一、基本操作题 1 【正确答案】 (1)MyClass(int i 10) (2)MyClass(const MyClass & p) (3) MyClass() 【试题解析】 (1)考查构造函数参数默认值,题目要求输出语句: The value is 10,从主函数中可以看出, obj1并没有初始化,但是 obj1调用 Print()函数时它的值为 10,由此可知构造函数的形参有默认值,且值为 10,因此得出语句MyClass(int i 10)。 (2)主要考查考生对复制构造函数的掌握,复制构造函数的形参都为引用,同时为了不改变形参的值要加上 con

14、st,因此得出语句 MyClass(const MyClass & p)。 (3)主要考查考生对析构函数的 掌握,析构函数和构造函数一样,前面不能添加任何类型,要把 void去掉。 二、简单应用题 2 【正确答案】 (1)getElement(i, j) (2)MatrixBase(rows, cols) (3)new doublerows*cols (4)r c 【试题解析】 (1)主要考查考生对纯虚函数的掌握,函数功能是分行显示矩阵中所有元素。因此在这里要输出行为 i、列为 j的元素,使用纯虚函数getElement(i, j)实现,输出语句为 cout getElement(i, j) “ “;。 (2)主要考查考生对派生类的构造函数的掌握,派生类的构造函数使用成员列表初始化法,先对基类初始化。 (3)主要考查考生对动态数组的掌握, val是 double型指针,要给 val赋值,就要先给它分配空间,应使用 new来完成。 (4)主要考查考生对成员函数的掌握,因为要输出单位矩阵,只有满足条件 rc的元素为 1 0,所以填写语句 if(r c)return 1 0;。 三、综合应用题 3 【正确答案】 for(int i 0; i len; i) 遍历对象 list中的数组和 d数组,把对应的值相加后放到数组 dd中。 ddi di list di;

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

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

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