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