1、C+语言笔试-13 及答案解析(总分:100.00,做题时间:90 分钟)一、操作题(总题数:3,分数:100.00)1.请使用 VC6 或使用答题菜单打开考生文件夹 projl 下的工程 proj1,此工程包含一个源程序文件proj1.cpp。其中位于每个注释“/ERROR*found*”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:You are nght.注意:只修改注释“/ERROR *found*”的下一行语句,不要改动程序中的其他内容。/proj1.cpp#include iostreamusing namespace std;class MyClasspublic:
2、MyClass(int x):number (x) /ERROR *found*MyClass(int x) /ERROR *found*void Judge(MyClass private:int number;void Judge(MyClass elsecout“Sorry“endl;int main()/ERROR *found*MyClass object;Judge(object);return 0;(分数:30.00)_2.请使用 VC6 或使用答题菜单打开考生文件夹 proj2 下的工程 proj2,此工程中包含一个头文件shape.h,其中包含了类 Shape、Point 和
3、 Triangle 的声明;包含程序文件 shape.cpp,其中包含了类Triangle 的成员函数和其他函数的定义;还包含程序文件 proj2.cpp,其中包含测试类 Shape、Point 和Triangle 的程序语句。请在程序中的横线处填写适当的代码并删除横线,以实现上述功能。此程序的正确输出结果应为:此图形是一个抽象图形,周长=0,面积=0此图形是一个三角形,周长=6.82843,面积=2注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“/*found*”。/shape.hclass Shapepublic:virtual double perimete
4、r()const return 0; /返回形状的周长virtual double area()const return 0;) /返回形状的面积virtual const char * name()constreturn“抽象图形“; /返回形状的名称;class Point /表示平面坐标系中的点的类double x;double y;public:/*found*Point (double x0, double y0):_/用 x0、y0 初始化数据成员 X、Ydouble getX() constreturn x;double getY() constreturn y;class Tr
5、iangle:public Shape/*found*_;/定义 3 个私有数据成员public:Triangle(Point p1,Point p2,Pointp3):point1(p1),point2(p2),point3(p3)double perimeter()const;double area () const;const char* name () constreturn “三角形“;/shape.cpp#include “shape.h“#include cmathdouble length(Point p1,Point p2)return sqrt(p1.getX()-p2.g
6、etX()*(p1.getX()-p2.getX()+(p1.getY()-p2.getY()*(p1.getY()-p2.getY();double Triangle:perimeter()const/一个 return 语句,它利用 length 函数计算并返回三角形的周长/*found*double Triangle:area()constdouble s=perimeter () /2.0;return sqrt (s* (s-length(point1,point2)*(s-length(point2,point3)*(s-length(point3,point1);/proj2.c
7、pp#include“shape.h“#includeiostreamusing namespace std;/*found*_/show 函数的函数头(函数体以前的部分)cout“此图形是一个“shape.name()“,周长=“shape.perimeter()“,面积=“shape.area()endl;int main()Shape S;Triangle tri (Point(0,2),Point(2,0),Point(0,0);show(s);show(tri);return 0;(分数:30.00)_3.请使用答题菜单命令或直接用 VC6 打开考生文件夹下的工程 proj3,其中声
8、明的是一个人员信息类,补充编制程序,使其功能完整。在 main 函数中给出了一组测试数据,此种情况下程序的输出应该是:Zhang 20 Tsinghua。注意:只能在函数 address_change 的“/*333*”和“/*666*”之间填入若干语句,不要改动程序中的其他内容。程序最后将结果输出到文件 out.dat 中。输出函数 writeToFile 已经编译为 obj 文件,并且在本程序中调用。/proj3.h#includeiostream#includestringusing namespace std;class Personchar name20;int age;char*
9、address;public:Person()age=0;address=0;void name_change(char*name);/名字修改函数void age_change(int_age);/年龄修改函数void address_change(char* add);/地址修改函数void info_display();/人员信息显示Person();/析构函数;void writeToFile (const char*path);proj3.cpp#include iostream#include string#include“proj 3.h“using namespace std;
10、void Person:name_change(char*_name)strcpy(name,_name);void Person:age_change(int_age)age=_age;void Person:address change(char*_add)if(address!=NULL) delete address;void Person:info_display () coutname/tif(address!=NULL)coutaddressendl;coutendl;Person:Person()if(address!=NULL)delete address;void main
11、()Person p1;p1.name_change(“Zhang“);p1.age_change(20);p1.address_change(“Tsinghua Uni-versity“);p1.address_change(“Tsinghua“);p1.info_display();writeToFile(“ “);(分数:40.00)_C+语言笔试-13 答案解析(总分:100.00,做题时间:90 分钟)一、操作题(总题数:3,分数:100.00)1.请使用 VC6 或使用答题菜单打开考生文件夹 projl 下的工程 proj1,此工程包含一个源程序文件proj1.cpp。其中位于每个
12、注释“/ERROR*found*”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:You are nght.注意:只修改注释“/ERROR *found*”的下一行语句,不要改动程序中的其他内容。/proj1.cpp#include iostreamusing namespace std;class MyClasspublic:MyClass(int x):number (x) /ERROR *found*MyClass(int x) /ERROR *found*void Judge(MyClass private:int number;void Judge(MyClass els
13、ecout“Sorry“endl;int main()/ERROR *found*MyClass object;Judge(object);return 0;(分数:30.00)_正确答案:(1)MyClass()(2)friend void Judge(MyClass (3)MyClass object(10);)解析:解析 主要考查的是 MyClass 类,其中涉及构造函数、析构函数和友元函数。(1)主要考查考生对析构函数的掌握,析构函数是没有形参的,因此把形参去掉即可,因为该类没有动态分配空间,所以不需要使用 delete 语句释放空间。(2)主要考查考生对友元函数的掌握,在函数定义中有
14、语句 void Judge(MyClass ,即 Judge 函数是可以被类外函数调用的,并且定义时没有加上类名和作用域,因此可以知道 Judge 函数是友元函数,需要在类型前加上 friend。(3)主要考查考生对构造函数调用的掌握,题目要求输出结果为:You are right.,在 Judge 函数体内有语句:if(obj.number=10)cout“You are right.“endl;要想使屏幕输出语句:You are right.,就必须使 obj.number 的值为 10,因此要使用构造函数语句MyClass object(10);。2.请使用 VC6 或使用答题菜单打开考
15、生文件夹 proj2 下的工程 proj2,此工程中包含一个头文件shape.h,其中包含了类 Shape、Point 和 Triangle 的声明;包含程序文件 shape.cpp,其中包含了类Triangle 的成员函数和其他函数的定义;还包含程序文件 proj2.cpp,其中包含测试类 Shape、Point 和Triangle 的程序语句。请在程序中的横线处填写适当的代码并删除横线,以实现上述功能。此程序的正确输出结果应为:此图形是一个抽象图形,周长=0,面积=0此图形是一个三角形,周长=6.82843,面积=2注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移
16、动“/*found*”。/shape.hclass Shapepublic:virtual double perimeter()const return 0; /返回形状的周长virtual double area()const return 0;) /返回形状的面积virtual const char * name()constreturn“抽象图形“; /返回形状的名称;class Point /表示平面坐标系中的点的类double x;double y;public:/*found*Point (double x0, double y0):_/用 x0、y0 初始化数据成员 X、Ydou
17、ble getX() constreturn x;double getY() constreturn y;class Triangle:public Shape/*found*_;/定义 3 个私有数据成员public:Triangle(Point p1,Point p2,Pointp3):point1(p1),point2(p2),point3(p3)double perimeter()const;double area () const;const char* name () constreturn “三角形“;/shape.cpp#include “shape.h“#include cm
18、athdouble length(Point p1,Point p2)return sqrt(p1.getX()-p2.getX()*(p1.getX()-p2.getX()+(p1.getY()-p2.getY()*(p1.getY()-p2.getY();double Triangle:perimeter()const/一个 return 语句,它利用 length 函数计算并返回三角形的周长/*found*double Triangle:area()constdouble s=perimeter () /2.0;return sqrt (s* (s-length(point1,point
19、2)*(s-length(point2,point3)*(s-length(point3,point1);/proj2.cpp#include“shape.h“#includeiostreamusing namespace std;/*found*_/show 函数的函数头(函数体以前的部分)cout“此图形是一个“shape.name()“,周长=“shape.perimeter()“,面积=“shape.area()endl;int main()Shape S;Triangle tri (Point(0,2),Point(2,0),Point(0,0);show(s);show(tri);
20、return 0;(分数:30.00)_正确答案:(1)x(x0),y(y0)(2)Point point1,point2,point3(3)return length(point1,point2)+length(point1,point3)+length(point2,point3)(4)void show(Shapeclass Personchar name20;int age;char* address;public:Person()age=0;address=0;void name_change(char*name);/名字修改函数void age_change(int_age);/年
21、龄修改函数void address_change(char* add);/地址修改函数void info_display();/人员信息显示Person();/析构函数;void writeToFile (const char*path);proj3.cpp#include iostream#include string#include“proj 3.h“using namespace std;void Person:name_change(char*_name)strcpy(name,_name);void Person:age_change(int_age)age=_age;void Pe
22、rson:address change(char*_add)if(address!=NULL) delete address;void Person:info_display () coutname/tif(address!=NULL)coutaddressendl;coutendl;Person:Person()if(address!=NULL)delete address;void main()Person p1;p1.name_change(“Zhang“);p1.age_change(20);p1.address_change(“Tsinghua Uni-versity“);p1.ad
23、dress_change(“Tsinghua“);p1.info_display();writeToFile(“ “);(分数:40.00)_正确答案:(address=new charstrlen(add)+1; /给类成员变量动态分配空间strcpy(address,_add);/把字符串 add 复制给 address)解析:解析 主要考查的是 Person 类,其中涉及动态数组、析构函数、构造函数和成员函数。函数 address_change(char*_add)的功能是地址修改,也就是说通过该函数把类的地址修改为 add 字符串。类的私有成员 address 是字符指针,因此首先要给 address 分配空间,通过 new 语句来实现:address=new charstrlen(_add)+1;。接下来就是要复制字符串,使用系统函数 strcpy,其用法为strcpy(参数 1,参数 2),将参数 2 的内容复制到参数 1 中。