问答题 请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2。其中的Collection定义了集合类的操作接口。一个集合对象可以包含若干元素。工程中声明的Array是一个表示整型数组的类,是Collection的派生类,它实现了Collection中声明的纯虚函数。Array的成员说明如下: 成员函数add用于向数组的末尾添加一个元素; 成员函数get用于获取数组中指定位置的元素; 数据成员a表示实际用于存储数据的整型数组; 数据成员size表示数组的容量,数组中的元素个数最多不能超过size; 数据成员num表示当前数组中的元素个数。 请在横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为: 1,2,3,4,5,6,7,8,9,10, 注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。 #include <iostream> using namespace std; //集合类的操作接口 class Collection { public: virtual void add (int e) = 0; //获取指定位置的元素 virtual int get (unsigned int i) const = 0; }; //实现了集合接口 class Array : public Collection { public: Array (unsigned int s) { //********** found********** a = new ______; size = s; num: 0; } ~Array ( ) { //********** found********** ______: } virtual void add (int e) { if (num< size) { //********** found********** ______=e; num ++; } } virtual int get ( unsigned int i) const { if (i < size) { //********** found********** ______; } return 0; } private: int * a; unsigned int size; unsigned int hum; }; void fun (Collection& col) { int i; for (i = 0; i < 10; i++) { col.add (i+1); } for (i = 0; i<10; i++) { cout << col.get(i) << ","; } cout << endl; } int main () { Array a (0xff); fun (a); return 0; }
【正确答案】(1)int[s] (2)delete[]a (3)a[nun] (4)return a[i]
【答案解析】[考点] 本题考查的是Collection类及其派生类Array类,其中涉及纯虚函数、构造函数和析构函数。 [解析] (1)主要考查考生对构造函数的掌握情况,要使用new给动态数组分配空间。 (2)主要考查考生对析构函数的掌握情况,使用delete释放空间。 (3)主要考查考生对成员函数的掌握情况,为数组添加元素,使用语句:a[Brim]=e;。 (4)主要考查考生对成员函数的掌握情况,返回数组元素。