1、二级 C+分类模拟 113 及答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.请使用 VC6 或使用【答题】菜单打开 proj1 下的工程 proj1,此工程中含有一个源程序文件proj1.cpp。其中位于每个注释“/ ERROR *found*”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为: Constructor called. The value is 10 Max number is 20 Destructor called. 注意:只能修改注释“/ ERROR *found*”的下一行语句,不要改动程序中的其他内容。
2、 / proj1.cpp #include iostream using namespace std; class MyClass public: / ERROR *found* void MyClass (int i) value = i; cout “Constructor called.“ endl; int Max (int x, int y) return xy ? x : y; /求两个整数的最大值 / ERROR *found* int Max(int x, int y, int z = 0) /求三个整数的最大值 if (x y) return xz ? x : z; else
3、 return yz ? y : z; int GetValue () const return value; MyClass () cout “Destructor called.“ endl; private: int value; ; int main() MyClass obj(10); / ERROR *found* cout “The value is“ value() endl; cout “Max number is“ obj.Max(10,20) endl; return 0; (分数:30.00)_二、简单应用题(总题数:1,分数:30.00)2.请使用 VC6 或使用【答
4、题】菜单打开 proj2 下的工程 proj2,该工程中含有一个源程序文件proj2.cpp,请将堆栈类的定义补充完整。使程序的输出结果为: The element of stack are: 4 3 2 1 注意:请勿修改主函数 main 和其他函数中的任何内容,只在横线处编写适当代码,不要改动程序中的其他内容,也不要删除或移动“/ *found*”。 /proj2.cpp #include iostream using namespace std; const int Size =5; class Stack; class Item public: / *found* Item(const
5、 int Item * next; friend class Stack; ; class Stack public: Stack():top(NULL) Stack(); int Pop(); void Push(const int private: Item * top; ; Stack: Stack() Item *p=top, *q; while (p!=NULL) q = p - next; / *found* _; /释放 p 所指向的节点 p = q; int Stack:Pop() Item * temp; int ret; / *found* _; /使 temp 指向栈顶节
6、点 ret = top - item; top = top - next; delete temp; return ret; void Stack:Push (const int / *found* _; /使新节点的 next 指针指向栈顶数据 top = temp; int main() Stack s; for(int i=1; iSize; i +) s. Push(i); cout “The element of stack are:“; for(i=1; iSize; i +) cout s.Pop() “/t“; return 0; (分数:30.00)_三、综合应用题(总题数:
7、1,分数:40.00)3.请使用 VC6 或使用【答题】菜单打开 proj3 下的工程文件 proj3,此工程中包含一个源程序文件proj3.cpp,其中定义了用于表示平面坐标系中的点的类 MyPoint 和表示矩形的类 MyRectangle;程序应当显示: (0,2)(2,2)(2,0)(0,0)4 但程序中有缺失部分,请按照以下提示,把缺失部分补充完整: (1)在“/ *1* *found*”的下方是构造函数的定义,它用参数提供的左上角和右下角的坐标对up_left 和 down_right 进行初始化。 (2)在“/ *2* *found*”的下方是成员函数 getDownLeft 的
8、定义中的一条语句。函数getDownLeft 返回用 MyPoint 对象表示的矩形的左下角。 (3)在“/ *3* *found*”的下方是成员函数 area 的定义,它返回矩形的面积。 注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“*found*”。/ proj3.cpp #include iostream using namespace std; class MyPoint /表示平面坐标系中的点的类 double x; double y; public: MyPoint (double x, double y) this-x=x; this-y=y; do
9、uble getX() const return x; double getY() const return y; void show () const cout “(“ x “,“ y“)“ ; ; class MyRectangle /表示矩形的类 MyPoint up_left; /矩形的左上角顶点 MyPoint downright; /矩形的右下角顶点 public: MyRectangle (MyPoint upleft, MyPoint downright); MyPoint getUpLeft() const return up_left; /返回左上角坐标 MyPoint g
10、etDownRight() const return down_right; /返回右下角坐标 MyPoint getUpRight() const; /返回右上角坐标 MyPoint getDownLeft() const; /返回左下角坐标 double area() const; /返回矩形的面积 ; / *1* *found* MyRectangle:MyRectangle (_): up_left (p1), down_right (p2) MyPoint MyRectangle:getUpRight()const return MyPoint (down_right.getX(),
11、 up_left.getY(); MyPoint MyRectangle:getDownLeft()const / *2* *found* return MyPoint(_); / *3* *found* _ area() const return (getUpLeft().getX()-getDownRight().getX ()*(getDownRight().getY()-getUpLeft().getY(); int main() MyRectangle r (MyPoint (0, 2), MyPoint(2,0); r.getUpLeft().show(); r.getUpRigh
12、t().show(); r.getDownRight().show(); r.getDownLeft().show(); cout r.area() endl; return 0; (分数:40.00)_二级 C+分类模拟 113 答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.请使用 VC6 或使用【答题】菜单打开 proj1 下的工程 proj1,此工程中含有一个源程序文件proj1.cpp。其中位于每个注释“/ ERROR *found*”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为: Constructor calle
13、d. The value is 10 Max number is 20 Destructor called. 注意:只能修改注释“/ ERROR *found*”的下一行语句,不要改动程序中的其他内容。 / proj1.cpp #include iostream using namespace std; class MyClass public: / ERROR *found* void MyClass (int i) value = i; cout “Constructor called.“ endl; int Max (int x, int y) return xy ? x : y; /求
14、两个整数的最大值 / ERROR *found* int Max(int x, int y, int z = 0) /求三个整数的最大值 if (x y) return xz ? x : z; else return yz ? y : z; int GetValue () const return value; MyClass () cout “Destructor called.“ endl; private: int value; ; int main() MyClass obj(10); / ERROR *found* cout “The value is“ value() endl;
15、cout “Max number is“ obj.Max(10,20) endl; return 0; (分数:30.00)_正确答案:()解析:(1)MyClass(int i) (2)int Max(int x,int y,int z) (3)cout“The value is“obj.GetValue()endl; 答案考生文件夹 考点 本题考查 MyClass 类、构造函数、析构函数、成员函数和函数重载。函数重载必须要求形参类型不同,或者形参个数不同。 解析 (1)考查构造函数,构造函数前不加 void 或其他任何类型名,直接使用 MyClass(int i)即可。 (2)主要考查函数
16、重载,在 int Max(int x,int y) return xy? x:y;中两个形参变量都是 int 型,而语句 int Max(int x,int y,int z=0)的前两个形参也都是 int 型,第三个形参定义默认值,那么这两个 Max 函数在调用时它们的参数个数和参数类型都一样,因为函数重载要求形参类型或形参个数不同,所以要把 int z = 0 改为 int z,才能构成函数重载。 (3)主要考查成员函数的调用,因为 value 是私有成员,所以不能被类外函数直接调用,而且 value()的用法也是错误的,可以使用成员函数 obj.GetValue()得到 value 的值。
17、二、简单应用题(总题数:1,分数:30.00)2.请使用 VC6 或使用【答题】菜单打开 proj2 下的工程 proj2,该工程中含有一个源程序文件proj2.cpp,请将堆栈类的定义补充完整。使程序的输出结果为: The element of stack are: 4 3 2 1 注意:请勿修改主函数 main 和其他函数中的任何内容,只在横线处编写适当代码,不要改动程序中的其他内容,也不要删除或移动“/ *found*”。 /proj2.cpp #include iostream using namespace std; const int Size =5; class Stack; c
18、lass Item public: / *found* Item(const int Item * next; friend class Stack; ; class Stack public: Stack():top(NULL) Stack(); int Pop(); void Push(const int private: Item * top; ; Stack: Stack() Item *p=top, *q; while (p!=NULL) q = p - next; / *found* _; /释放 p 所指向的节点 p = q; int Stack:Pop() Item * tem
19、p; int ret; / *found* _; /使 temp 指向栈顶节点 ret = top - item; top = top - next; delete temp; return ret; void Stack:Push (const int / *found* _; /使新节点的 next 指针指向栈顶数据 top = temp; int main() Stack s; for(int i=1; iSize; i +) s. Push(i); cout “The element of stack are:“; for(i=1; iSize; i +) cout s.Pop() “
20、/t“; return 0; (分数:30.00)_正确答案:()解析:(1)item(val) (2)deletep (3)temp=top (4)temp-next=top 答案考生文件夹 考点 本题考查堆栈类 Stack 类、Item 类、构造函数、析构函数、成员函数和函数调用。堆栈类的节点一般使用指针表示,也就会考查到指针的相关知识点,要注意释放指针应使用 delete语句。 解析 (1)主要考查构造函数,对私有成员进行初始化,即 item(val)。 (2)主要考查使用 delete 语句释放指针,一般格式为:delete+指针。 (3)指向栈顶节点的是 top 指针,要使 temp
21、 指向栈顶节点,故使用语句 temp=top;。 (4)指向栈顶节点的是 top 指针,要使新节点的 next 指针指向栈顶数据,故使用语句 temp-next=top;。 本题涉及堆栈类,栈是先进后出,后进先出的存储结构。对于此类问题指针的使用是个难点,要记住栈中指向栈顶节点的是 top 指针,添加数据时要往栈顶添加。三、综合应用题(总题数:1,分数:40.00)3.请使用 VC6 或使用【答题】菜单打开 proj3 下的工程文件 proj3,此工程中包含一个源程序文件proj3.cpp,其中定义了用于表示平面坐标系中的点的类 MyPoint 和表示矩形的类 MyRectangle;程序应当
22、显示: (0,2)(2,2)(2,0)(0,0)4 但程序中有缺失部分,请按照以下提示,把缺失部分补充完整: (1)在“/ *1* *found*”的下方是构造函数的定义,它用参数提供的左上角和右下角的坐标对up_left 和 down_right 进行初始化。 (2)在“/ *2* *found*”的下方是成员函数 getDownLeft 的定义中的一条语句。函数getDownLeft 返回用 MyPoint 对象表示的矩形的左下角。 (3)在“/ *3* *found*”的下方是成员函数 area 的定义,它返回矩形的面积。 注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要
23、删除或移动“*found*”。/ proj3.cpp #include iostream using namespace std; class MyPoint /表示平面坐标系中的点的类 double x; double y; public: MyPoint (double x, double y) this-x=x; this-y=y; double getX() const return x; double getY() const return y; void show () const cout “(“ x “,“ y“)“ ; ; class MyRectangle /表示矩形的类
24、MyPoint up_left; /矩形的左上角顶点 MyPoint downright; /矩形的右下角顶点 public: MyRectangle (MyPoint upleft, MyPoint downright); MyPoint getUpLeft() const return up_left; /返回左上角坐标 MyPoint getDownRight() const return down_right; /返回右下角坐标 MyPoint getUpRight() const; /返回右上角坐标 MyPoint getDownLeft() const; /返回左下角坐标 doub
25、le area() const; /返回矩形的面积 ; / *1* *found* MyRectangle:MyRectangle (_): up_left (p1), down_right (p2) MyPoint MyRectangle:getUpRight()const return MyPoint (down_right.getX(), up_left.getY(); MyPoint MyRectangle:getDownLeft()const / *2* *found* return MyPoint(_); / *3* *found* _ area() const return (g
26、etUpLeft().getX()-getDownRight().getX ()*(getDownRight().getY()-getUpLeft().getY(); int main() MyRectangle r (MyPoint (0, 2), MyPoint(2,0); r.getUpLeft().show(); r.getUpRight().show(); r.getDownRight().show(); r.getDownLeft().show(); cout r.area() endl; return 0; (分数:40.00)_正确答案:()解析:(1)MyPoint p1,M
27、yPoint p2 (2)up_left.getX(),down_right.getY() (3)double MyRectangle: 答案考生文件夹 考点 本题考查表示平面坐标系中的点的类 MyPoint、表示矩形的类 MyRectangle、构造函数和成员函数。 解析 (1)考查构造函数,构造函数中的参数要给私有成员赋值,在下句中 up_left(p1),down_right(p2)指出私有成员赋值要使用形参 p1 和 p2,因此这里参数要定义为 MyPoint p1,MyPoint p2。 (2)主要考查成员函数的返回语句,MyPoint My Rectangle:getDownLeft()const 函数要求返回一个左下角的点坐标,因此使用语句 MyPoint(up_left.getX(),down_right.getY();。 (3)主要考查成员函数的定义,在 MyRectangle 类中已经声明 double area()const,因此此处只要添加double MyRectangle:即可。 构造函数的参数定义时要注意在赋值语句中使用的参数。考查构造函数一般都会考查到形参,应注意联系上下文。类的成员函数在类外定义时要在函数名前面加上:返回值类型+类名+作用域(:)。