1、二级 C+分类模拟 129 及答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.请使用 VC6 或使用【答题】菜单打开 proj1 下的工程 proj1,该工程中包含程序文件 main.cpp,其中有类 Foo 和主函数 main 的定义。程序中位于每个“/ ERROR *found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是: X=a Y=42 注意:只修改每个“/ ERROR *found*”下的那一行,不要改动程序中的其他内容。 #include iostream using namespace std; class
2、 Foo public: Foo(char x) x_ = x; char getX() const return x_; public: static int y_; private: char x_; ; / ERROR *found* int Foo.y_ = 42; int main(int argc, char * argv) / ERROR *found* Foo f; / ERROR *found* cout “X =“ f.x_ endl; cout “Y =“ f.y_ endl; return 0; (分数:30.00)_二、简单应用题(总题数:1,分数:30.00)2.请
3、使用 VC6 或使用【答题】菜单打开 proj2 下的工程 proj2。其中有类 Point(“点”)、Rectangle(“矩形”)和 Circle(“圆”)的定义。在程序所使用的平面坐标系统中,x 轴的正方向是水平向右的,y 轴的正方向是竖直向下的。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应该是: -圆形- 圆心=(3,2) 半径=1 面积=3.14159 -外切矩形- 左上角=(2,1) 右下角=(4,3) 面积=4 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“/ *found*”。 #include iostream
4、#include cmath using namespace std; /平面坐标中的点 /本题坐标系统中,x 轴的正方向水平向右,y 轴的正方向竖直向下。 class Point public: Point(double x=0.0, double y=0.0): x_(x), y_(y) double getX() const return x_; double getY() const return y_; void setX(double x) x_=x; void setY(double y) y_=y; private: double x_; /x 坐标 double y_; /y
5、 坐标 ; /矩形 class Rectangle public: Rectangle (Point p, int w, int h) : point (p), width (w), height (h) double area() const /矩形面积 return width * height; Point topLeft() const /左上角顶点 return point; Point bottomRight() const /右下角顶点(注:y 轴正方向竖直向下) / *found* return Point(_); private: Point point; /左上角顶点 do
6、uble width; /水平边长度 double height; /垂直边长度 ; /圆形 class Circle public: Circle (Point p, double r):center(p), radius(r) Rectangle boundingBox() const; /外切矩形 double area() const /圆形面积 / *found* return PI *_; public: static const double PI; /圆周率 private: Point center; /圆心 double radius; /半径 ; const double
7、 Circle:PI = 3.14159; Rectangle Circle:boundingBox() const / *found* Point pt(_); int w, h; / *found* w = h =_; return Rectangle(pt, w, h); int main () Point p(3, 2); Circle c(p, 1); cout -圆形- /n“; cout “圆心 = (“ p.getX() “,“ p.getY() “)/n“; cout “半径 =“ 1 endl; cout “面积 =“ c.area() endl endl; Rectang
8、le bb = c.boundingBox(); Point t1 = bb.topLeft(); Point br = bb.bottomRight(); cout “-外切矩形-/n“; cout “左上角 = (“ t1.getX() “,“ t1.getY() “)/n“; cout “右下角 = (“ br.getX() “,“ br.getY() “)/n“; cout “面积 =“ bb.area() endl; return 0; (分数:30.00)_三、综合应用题(总题数:1,分数:40.00)3.请使用 VC6 或使用【答题】菜单打开 proj3 下的工程 proj3,其
9、中定义了 MyString 类,一个用于表示字符串的类。成员函数 reverse 的功能是将字符串进行“反转”。例如,将字符串 ABCDEF“反转”后,得到字符串 FEDCBA;将字符串 ABCDEFG“反转”后,得到字符串 GFEDCBA。请编写成员函数 reverse。在main 函数中给出了一组测试数据,此时程序运行中应显示: 读取输入文件. -反转前- STR1=ABCDEF STR2=ABCDEFG -反转后- STR1=FEDCBA STR2=GFEDCBA 要求: 补充编制的内容写在“/ *333*”与“/ *666*”之间,不得修改程序的其他部分。 注意:程序最后将结果输出到文
10、件 out.dat 中,输出函数 WriteToFile 已经编译为 obj 文件,并且在本程序中调用。 /mgsering.h #include iostream #include cstring using namespace std; class MyString public: MyString(const char * s) str = new charstrlen (s) + 1; strcpy(str, s); MyString() delete str; void reverse(); friend ostream return os; private: char * str;
11、 ; void writeToFile(char *, const MyString /main.cpp #include “mystring.h“ #include fstream void MyString:reverse() / *333* / *666* int main () char inname128, pathname80; strcpy(pathname, “ “); sprintf(inname, “in.dat“, pathname); cout “读取输入文件. /n/n“; ifstream infile(inname); if(infile.fail() cerr
12、“打开输入文件失败!“; exit(1); char buf4096; infile.getline(buf, 4096) ; MyString str1(“ABCDEF“), str2(“ABCDEFG“), str3(buf); cout “-反转前-/n“; cout “STR1 =“ str1 endl; cout “STR2 =“ str2 endl endl; str1.reverse(); str2.reverse(); str3.reverse(); cout “-反转后-/n“; cout “STR1 =“ str1 endl; cout “STR2 =“ str2 endl
13、 endl; writeToFile (pathname, str3); return 0; (分数:40.00)_二级 C+分类模拟 129 答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.请使用 VC6 或使用【答题】菜单打开 proj1 下的工程 proj1,该工程中包含程序文件 main.cpp,其中有类 Foo 和主函数 main 的定义。程序中位于每个“/ ERROR *found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是: X=a Y=42 注意:只修改每个“/ ERROR *found*”下的那一行,不
14、要改动程序中的其他内容。 #include iostream using namespace std; class Foo public: Foo(char x) x_ = x; char getX() const return x_; public: static int y_; private: char x_; ; / ERROR *found* int Foo.y_ = 42; int main(int argc, char * argv) / ERROR *found* Foo f; / ERROR *found* cout “X =“ f.x_ endl; cout “Y =“ f
15、.y_ endl; return 0; (分数:30.00)_正确答案:()解析:(1)int Foo:y_=42; (2)Foo f(“a“); (3)cout “X =“ f.getX() endl; 答案考生文件夹 考点 本题考查的是 Foo 类,其中涉及构造函数、const 函数和静态成员。给类的静态成员赋值时要加上类名和作用域符号,与类的成员函数一样,类的私有成员不能被类外函数调用。 解析 (1)主要考查考生对静态成员的掌握,因为静态整型变量 y_是 Foo 类的公有成员,所以给 y_赋值时要加上“Foo:”,即 int Foo:y_=42;。 (2)主要考查考生对构造函数的掌握,题
16、目要求程序输出: X=a Y=42 可以知道,在给 Foo 类的 f 声明时要同时初始化为字符 a,即语句 Foo f(“a“);。 (3)主要考查考生对成员函数的掌握,因为 x 是类 Foo 的私有成员,所以不能在 main 函数中直接调用,要通过公有成员函数 getX()调用。 类的静态成员和类的成员函数一样,赋值时要加上类名和作用域符号,要注意通过观察题目对程序输出结果的要求,来给类赋初始值。二、简单应用题(总题数:1,分数:30.00)2.请使用 VC6 或使用【答题】菜单打开 proj2 下的工程 proj2。其中有类 Point(“点”)、Rectangle(“矩形”)和 Circ
17、le(“圆”)的定义。在程序所使用的平面坐标系统中,x 轴的正方向是水平向右的,y 轴的正方向是竖直向下的。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应该是: -圆形- 圆心=(3,2) 半径=1 面积=3.14159 -外切矩形- 左上角=(2,1) 右下角=(4,3) 面积=4 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“/ *found*”。 #include iostream #include cmath using namespace std; /平面坐标中的点 /本题坐标系统中,x 轴的正方向水平向右,y 轴的正方向
18、竖直向下。 class Point public: Point(double x=0.0, double y=0.0): x_(x), y_(y) double getX() const return x_; double getY() const return y_; void setX(double x) x_=x; void setY(double y) y_=y; private: double x_; /x 坐标 double y_; /y 坐标 ; /矩形 class Rectangle public: Rectangle (Point p, int w, int h) : poi
19、nt (p), width (w), height (h) double area() const /矩形面积 return width * height; Point topLeft() const /左上角顶点 return point; Point bottomRight() const /右下角顶点(注:y 轴正方向竖直向下) / *found* return Point(_); private: Point point; /左上角顶点 double width; /水平边长度 double height; /垂直边长度 ; /圆形 class Circle public: Circl
20、e (Point p, double r):center(p), radius(r) Rectangle boundingBox() const; /外切矩形 double area() const /圆形面积 / *found* return PI *_; public: static const double PI; /圆周率 private: Point center; /圆心 double radius; /半径 ; const double Circle:PI = 3.14159; Rectangle Circle:boundingBox() const / *found* Poin
21、t pt(_); int w, h; / *found* w = h =_; return Rectangle(pt, w, h); int main () Point p(3, 2); Circle c(p, 1); cout -圆形- /n“; cout “圆心 = (“ p.getX() “,“ p.getY() “)/n“; cout “半径 =“ 1 endl; cout “面积 =“ c.area() endl endl; Rectangle bb = c.boundingBox(); Point t1 = bb.topLeft(); Point br = bb.bottomRig
22、ht(); cout “-外切矩形-/n“; cout “左上角 = (“ t1.getX() “,“ t1.getY() “)/n“; cout “右下角 = (“ br.getX() “,“ br.getY() “)/n“; cout “面积 =“ bb.area() endl; return 0; (分数:30.00)_正确答案:()解析:(1)point.getX()+width,point.getY()+height (2)radius*radius (3)center.getX()-radius,center.getY()-radius (4)2*radius 答案考生文件夹 考点
23、 本题考查 Point 类、Rectangle 类和 Circle 类,其中涉及构造函数、const 函数和静态成员。 解析 (1)主要考查考生对成员函数的掌握,程序要求返回右下角顶点,该点的 x 坐标为左上角顶点的 x坐标加上 width,该点的 y 坐标为左上角顶点 y 坐标加上 height,即 return Point(point.getX()+width,point.getY()+height);。 (2)主要考查考生对成员函数的掌握,程序要求计算圆形面积,也就是返回圆面积,即 return PI*radius*radius;。 (3)主要考查考生对成员函数的掌握,首先看函数声明:R
24、ectangle Circle:boundingBox() const,可知该函数要返回的是一个 Rectangle 类型,即要返回的是圆的外切矩形。再看 Rectangle 类的构造函数Rectangle(Point p,int w,int h),由此可知,空格处要定义的点 pt 为左上角点,即 Point pt(center.getX()-radius,center.getY()-radius);。 (4)由函数声明和 Rectangle 类的构造函数可知,w 和 h 应该为直径,即 w=h=2*radius;。三、综合应用题(总题数:1,分数:40.00)3.请使用 VC6 或使用【答题
25、】菜单打开 proj3 下的工程 proj3,其中定义了 MyString 类,一个用于表示字符串的类。成员函数 reverse 的功能是将字符串进行“反转”。例如,将字符串 ABCDEF“反转”后,得到字符串 FEDCBA;将字符串 ABCDEFG“反转”后,得到字符串 GFEDCBA。请编写成员函数 reverse。在main 函数中给出了一组测试数据,此时程序运行中应显示: 读取输入文件. -反转前- STR1=ABCDEF STR2=ABCDEFG -反转后- STR1=FEDCBA STR2=GFEDCBA 要求: 补充编制的内容写在“/ *333*”与“/ *666*”之间,不得修
26、改程序的其他部分。 注意:程序最后将结果输出到文件 out.dat 中,输出函数 WriteToFile 已经编译为 obj 文件,并且在本程序中调用。 /mgsering.h #include iostream #include cstring using namespace std; class MyString public: MyString(const char * s) str = new charstrlen (s) + 1; strcpy(str, s); MyString() delete str; void reverse(); friend ostream return
27、os; private: char * str; ; void writeToFile(char *, const MyString /main.cpp #include “mystring.h“ #include fstream void MyString:reverse() / *333* / *666* int main () char inname128, pathname80; strcpy(pathname, “ “); sprintf(inname, “in.dat“, pathname); cout “读取输入文件. /n/n“; ifstream infile(inname)
28、; if(infile.fail() cerr “打开输入文件失败!“; exit(1); char buf4096; infile.getline(buf, 4096) ; MyString str1(“ABCDEF“), str2(“ABCDEFG“), str3(buf); cout “-反转前-/n“; cout “STR1 =“ str1 endl; cout “STR2 =“ str2 endl endl; str1.reverse(); str2.reverse(); str3.reverse(); cout “-反转后-/n“; cout “STR1 =“ str1 endl;
29、 cout “STR2 =“ str2 endl endl; writeToFile (pathname, str3); return 0; (分数:40.00)_正确答案:()解析:int length=strlen(str); /把字符串 str 的长度赋值给 lenth for(int i=0,j=length-1;ij;i+,j-) /从 i=0,j=length-1,ij 为条件开始遍历,并把Stri和 Strj交换 char temp = stri; /给定义的临时变量 temp 赋值为 stri stri = strj; /给 Stri赋值 Strj strj = temp; /
30、给 Strj赋值为 temp 答案考生文件夹 考点 主要考查 MyString 类,其中涉及动态数组、构造函数、析构函数、成员函数和友元函数。本题要把动态数组中的字符串反转过来,用两个变量 i 和 j 来代表要调换的元素的下标,再通过中间变量 temp 进行交换即可。 解析 主要考查考生对动态数组的掌握,先看题目要求:成员函数 reverse 的功能是将字符串进行“反转”。再由类的定义可知,字符串存放在动态数组 str 中,由 strlen 函数得出字符串的长度,最后一个字符的下标为 length-1,第一个字符的下标为 0,将这两个字符交换,然后 j 依次减 1 同时 i 依次加 1,继续交换,直到 i 大于 j 时停止循环即可。