【计算机类职业资格】二级C++分类模拟115及答案解析.doc
《【计算机类职业资格】二级C++分类模拟115及答案解析.doc》由会员分享,可在线阅读,更多相关《【计算机类职业资格】二级C++分类模拟115及答案解析.doc(9页珍藏版)》请在麦多课文档分享上搜索。
1、二级 C+分类模拟 115 及答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.请使用 VC6 或使用【答题】菜单打开 proj1 下的工程 proj1,此工程包含有一个源程序文件proj1.cpp。其中位于每个注释“/ ERROR *found*”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为: (4,4) 注意:只修改注释“/ ERROR *found*”的下一行语句,不要改动程序中的其他内容。 / proj1.cpp #include iostream using namespace std; class Point pub
2、lic: / ERROR *found* Point(double x, double y) _x(x), _y(y) double GetX() const return _x; double GetY() const return _y; / ERROR *found* void Move (double xOff, double yOff) const _x + = xOff; _y + = yOff; protected: double _x, _y; ; int main() Point pt(1.5, 2.5); pt.Move(2.5, 1.5); / ERROR *found*
3、 以下语句输出 pt 成员_x 和_y 的值 cout “(“ pt._x “,“ pt._y “)“ endl; return 0; (分数:30.00)_二、简单应用题(总题数:1,分数:30.00)2.请使用 VC6 或使用【答题】菜单打开 proj2 下的工程 proj2,此工程包含有一个源程序文件proj2.cpp。其中定义了 Base1 类、Base2 类和 Derived 类。 Base1 是一个抽象类,其类体中声明了纯虚函数 Show。Base2 类的构造函数负责动态分配一个字符数组,并将形参指向的字符串复制到该数组中,复制功能要求通过调用 strcpy 函数来实现。Deriv
4、ed 类以公有继承方式继承 Base1 类,以私有继承方式继承 Base2 类。在 Derived 类的构造函数的成员初始化列表中调用 Base 类的构造函数。 请在程序中的横线处填写适当的代码,然后删除横线,以完成 Base1、Base2 和 Derived 类的功能。此程序的正确输出结果应为: I“m a derived class. 注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“/ *found*”。 / proj2.cpp #include iostream #include cstring using namespace std; class Base1
5、 public: / *found* 下列语句需要声明纯虚函数 Show _; ; class Base2 protected: char * _p; Base2 (const char * s) _p = new charstrlen (s) +i; / *found* 下列语句将形参指向的字符串常量复制到该类的字符数组中 Base2() delete _p; ; / *found* Derived 类公有继承 Base1,私有继承 Base2 类 class Derived:_ public: / *found* 以下构造函数调用 Base2 类构造函数 Derived (const ch
6、ar * s):_ void Show() cout _p endl; ; int main() Base1 * pb = new Derived(“I“m a derived class.“); pb - Show(); delete pb; return 0; (分数:30.00)_三、综合应用题(总题数:1,分数:40.00)3.请使用 VC6 或使用【答题】菜单打开 proj3 下的工程文件 proj3,此工程包含一个源程序文件proj3.cpp,其功能是从文本文件 in.dat 中读取全部整数,将整数序列存放到 intArray 类的对象myArray 中,然后对整数序列按非递减排序
7、,最后由函数 writeToFile 选择序列中的部分数据输出到文件out.dat 中。文件 in.dat 中的整数个数不大于 300 个。 要求: 补充编制的内容写在“/ *333*”与“/ *666*”两行之间。实现对整数序列按非递减排序,并将排序结果在屏幕上输出。不得修改程序的其他部分。 注意:程序最后已将结果输出到文件 out.dat 中。输出函数 writeToFile 已经给出并且调用。 / proj3.cpp #include iostream #include fstream #include cstring using namespace std; class intArra
8、y private: int * array; /整数序列首地址 int length; /序列中的整数个数 public: /构造函数,从文件中读取数据用于初始化新对象。参数是文件名 intArray(char * filename); void sort(); /对整数序列按非递减排序 intArray(); void writeToFile (char * filename); ; intArray: intArray (char * filename) ifstream myFile(filename); int len=300; array = new intlen; length
9、= 0; while(myFile arraylength+); length-; myFile.close(); void intArray:sort() / *333* / *666* intArray: intArray() delete array; void intArray: writeToFile (char * filename) int step=0; ofstream outFile(filename); for(int i=0; ilength; i=i+step) outFile array i endl; step +; outFile.close(); void m
10、ain() intArray myArray (“in.dat“); myArray.sort(); myArray, writeToFile (“out.dat“); (分数:40.00)_二级 C+分类模拟 115 答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.请使用 VC6 或使用【答题】菜单打开 proj1 下的工程 proj1,此工程包含有一个源程序文件proj1.cpp。其中位于每个注释“/ ERROR *found*”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为: (4,4) 注意:只修改注释“/ ERROR
11、*found*”的下一行语句,不要改动程序中的其他内容。 / proj1.cpp #include iostream using namespace std; class Point public: / ERROR *found* Point(double x, double y) _x(x), _y(y) double GetX() const return _x; double GetY() const return _y; / ERROR *found* void Move (double xOff, double yOff) const _x + = xOff; _y + = yOff
12、; protected: double _x, _y; ; int main() Point pt(1.5, 2.5); pt.Move(2.5, 1.5); / ERROR *found* 以下语句输出 pt 成员_x 和_y 的值 cout “(“ pt._x “,“ pt._y “)“ endl; return 0; (分数:30.00)_正确答案:()解析:(1) Point(double x,double y):_x(x),_y(y)或 Point(double x,double y) _x=x;_y=y; (2)void Move(double xOff,double yOff)
13、(3)cout “(“ pt.GetX() “,“ pt.GetY() “)“ endl; 答案考生文件夹 考点 本题主要考查 Point 类,其中涉及构造函数、成员函数及成员函数的调用。构造函数的语法经常考查到,一般会考查形参的类型及名称,本题考查的比较特别,是考查函数成员初始化列表的基本知识。 解析 (1)主要考查的是构造函数的成员初始化列表的语法,在成员列表之前必须加“:”。 (2)主要考查成员函数中 const 的使用,先看 Move 函数的函数体: _x+=xOff;_y+=yOff; 可以看到 Point 类的两个私有成员_x 和_y 的值都发生了变化,因此 Move 函数不能使用
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
5000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 计算机 职业资格 二级 分类 模拟 115 答案 解析 DOC
