1、二级 C+分类模拟 126 及答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.请使用 VC6 或使用【答题】菜单打开 proj1 下的工程 proj1。程序中位于每个“/ ERROR *found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: value = 63 number = 1 注意:只修改每个“/ ERROR *found*”下的那一行,不要改动程序中的其他内容。 #include iostream using namespace std; class MyClass int * p; const int N;
2、public: / ERROR *found* MyClass(int val):N = 1 p = new int; *p = val; / ERROR *found* MyClass() delete *p; friend void print (MyClass ; / ERROR *found* void MyClass:print (MyClass cout “number =“ obj.N endl; int main() MyClass obj(63); print(obj); return 0; (分数:30.00)_二、简单应用题(总题数:1,分数:30.00)2.请使用 VC
3、6 或使用【答题】菜单打开 proj2 下的工程 proj2,其中定义了 Component 类、Composite 类和 Leaf 类。Component 是抽象基类,Composite 和 Leaf 是 Component 的公有派生类。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为: Leaf Node 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“/ *found*”。 #include iostream using namespace std; class Component public: /声明纯虚函数 print(
4、) / *found* _ ; class Composite:public Component public: / *found* void setChild(_) m_child = child; virtual void print() const m_child - print(); private: Component * m_child; ; class Leaf:public Component public: virtual void print() const / *found* _ ; int main() Leaf node; Composite comp; comp.s
5、etChild( Component * p = p - print(); return 0; (分数:30.00)_三、综合应用题(总题数:1,分数:40.00)3.请使用 VC6 或使用【答题】菜单打开 proj3 下的工程 proj3,其中定义的 Matrix 是一个用于表示矩阵的类。成员函数 max_value 的功能是求出所有矩阵元素中的最大值。例如,若有 33 矩阵 (分数:40.00)_二级 C+分类模拟 126 答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.请使用 VC6 或使用【答题】菜单打开 proj1 下的工程 pr
6、oj1。程序中位于每个“/ ERROR *found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: value = 63 number = 1 注意:只修改每个“/ ERROR *found*”下的那一行,不要改动程序中的其他内容。 #include iostream using namespace std; class MyClass int * p; const int N; public: / ERROR *found* MyClass(int val):N = 1 p = new int; *p = val; / ERROR *found* MyClass() del
7、ete *p; friend void print (MyClass ; / ERROR *found* void MyClass:print (MyClass cout “number =“ obj.N endl; int main() MyClass obj(63); print(obj); return 0; (分数:30.00)_正确答案:()解析:(1)MyClass(int val):N(1) (2)MyClass() delete p; (3)void print(MyClass class Component public: /声明纯虚函数 print() / *found*
8、_ ; class Composite:public Component public: / *found* void setChild(_) m_child = child; virtual void print() const m_child - print(); private: Component * m_child; ; class Leaf:public Component public: virtual void print() const / *found* _ ; int main() Leaf node; Composite comp; comp.setChild( Com
9、ponent * p = p - print(); return 0; (分数:30.00)_正确答案:()解析:(1)virtual void print() const=0; (2)Component*child (3)cout “Leaf Node“ endl; 答案考生文件夹 考点 本题考查抽象类 Component 类及其派生类 Composite 和 Leaf,其中涉及纯虚函数和成员函数。纯虚函数要根据在派生类中该函数的返回值、参数、有无 const 来确定。 解析 (1)主要考查考生对纯虚函数的掌握,题目要求声明纯虚函数 print()。在其派生类中 print()函数的定义为
10、virtual void print() const,由此可知纯虚函数为 virtual void print() const=0。 (2)主要考查考生对成员函数的掌握,题目要求填写函数 void setChild 的形参,由 setChild 的函数体可知形参为 child,再看类的私有成员 m_child 的定义:Component*m_child;。由此可知形参为:Component*child。 (3)主要考查考生对纯虚函数的掌握,先看主函数的程序: Leaf node; Composite comp; comp.setChild( Component* p = p-print();
11、第一条和第二条语句都是定义语句,第三条语句调用函数 setChild,由 setChild 函数的定义可知,comp中的 m_child 等于 node,第四条语句定义了个指针 p 指向 comp 的地址,也就是 node,最后一条语句通过指针 p 调用函数 print,也就是调用类 Leaf 的函数 print,因为题目要求输出:Leaf Node,因此在这里添加语句:cout“Lear Node“endl;。三、综合应用题(总题数:1,分数:40.00)3.请使用 VC6 或使用【答题】菜单打开 proj3 下的工程 proj3,其中定义的 Matrix 是一个用于表示矩阵的类。成员函数
12、max_value 的功能是求出所有矩阵元素中的最大值。例如,若有 33 矩阵 (分数:40.00)_正确答案:()解析:int temp = 0; /定义整数变量 temp,并赋值为零 for (int i = 0; i M; i +) /遍历矩阵的行 for (int j = 0; j N; j +) /遍历短阵的列 if (temp arrayij) /如果 temp 小于 arrayij temp = arrayij; /把 arrayij赋值给 temp return temp; /返回 temp 答案考生文件夹 考点 本题考查 Matrix 类,其中涉及构造函数、二维数组、成员函数和 const 函数。 解析 主要考查考生对二维数组的掌握,题目要求成员函数 max_value 的功能是求出所有矩阵元素中的最大值。因此只要逐个元素比较即可,下标 i 和 j 作为矩阵行和列的标记,使用双层 for 循环来遍历数组中的所有元素。 主要考查考生对二维数组的掌握,二维数组使得存储的数据大幅增加,只要把二维数组想象成矩阵,利用矩阵相关知识求解即可。