1、二级 C+分类模拟 120 及答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.请使用 VC6 或使用【答题】菜单打开 proj1 下的工程 proj1,此工程包含一个源程序文件 proj1.cpp。其中位于每个注释“/ ERROR *found*”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:You are fight. 注意:只修改注释“/ERROR *found*”的下一行语句,不要改动程序中的其他内容。 / proj1.cpp #include iostream using namespace std; class My
2、Class public: MyClass(int x):number(x) / ERROR *found* MyClass(int x) / ERROR *found* void Judge(MyClass private: int number; ; void Judge(MyClass else cout “Sorry“ endl; int main() / ERROR *found* MyClass object; Judge(object); return 0; (分数:30.00)_二、简单应用题(总题数:1,分数:30.00)2.请使用 VC6 或使用【答题】菜单打开 proj2
3、 下的工程 proj2,此工程中包含一个头文件 shape.h,其中包含了类 Shape、Point 和 Triangle 的声明;包含程序文件 shape.cpp,其中包含了类 Triangle 的成员函数和其他函数的定义;还包含程序文件 proj2.cpp,其中包含测试类 Shape、Point 和 Triangle 的程序语句。请在程序中的横线处填写适当的代码并删除横线,以实现上述功能。此程序的正确输出结果应为: 此图形是一个抽象图形,周长=0,面积=0 此图形是一个三角形,周长=6.82843,面积=2 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“/
4、*found*”。 /shape.h class Shape public: virtual double perimeter() const return 0; /返回形状的周长 virtual double area() const return 0; /返回形状的面积 virtual const char * name() const return “抽象图形“; /返回形状的名称 ; class Point /表示平面坐标系中点的类 double x; double y; public: / *found* Point (double x0, double y0): _ /用 x0、y
5、0 初始化数据成员 x、y double getX() const return x; double getY() const return y; ; class Triangle: public Shape / *found* _; /定义 3 个私有数据成员 public: Triangle (Point p1, Point p2, Point p3): point1 (p1), point2 (p2), point3 (p3) double perimeter () const; double area() const; const char * name() const return
6、“三角形“; ; /shape.cpp #include “shape.h“ #include cmath double 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:
7、area() const double s = perimeter()/2.0; return sqrt (s * (s - length (point1, point2) * (s - length (point2, point3) * (s - length (point3, point1); /proj2.cpp #include “shape.h“ #include iostream using namespace std; / *found* _ /show 函数的函数头(函数体以前的部分) cout “此图形是一个“ shape.name() “,周长=“ shape.perime
8、ter() “,面积=“ 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)_三、综合应用题(总题数:1,分数:40.00)3.请使用【答题】菜单命令或直接用 VC6 打开考生文件夹下的工程 proj3,其中声明的是一个人员信息类,补充编制程序,使其功能完整。在 main 函数中给出了一组测试数据,此种情况下程序的输出应该是:Zhang 20 Tsinghua。 注意:只能在函数 addre
9、ss_change 的“/ *333*”和“/ *666*”之间填入若干语句,不要改动程序中的其他内容。 程序最后将结果输出到文件 out.dat 中。输出函数 writeToFile 已经编译为 obj 文件,并且在本程序中调用。 /proj3.h #include iostream #include string using namespace std; class Person char name20; int age; char * address; public: Person() age = 0;address = 0; void name_change (char *_name)
10、; /名字修改函数 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 “proj3.h“ using namespace std; void Person:name_change(char*_name) strcpy(nam
11、e, _name); void Person:age_change(int _age) age = _age; void Person:address _change (char * add) if (address!=NULL) deleteaddress; / *333* / *666* void Person:info_display() cout name “/t“ age “/t“; if(address!=NULL) cout address endl; cout endl; Person:Person() if(address!=NULL) delete address; voi
12、d main() Person p1; p1.name_change(“Zhang“); p1.age_change(20); p1.address_change(“Tsinghua University“); p1.address_change(“Tsinghua“); p1.info_display(); writeToFile(“ “); (分数:40.00)_二级 C+分类模拟 120 答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.请使用 VC6 或使用【答题】菜单打开 proj1 下的工程 proj1,此工程包含一个源程序文件
13、proj1.cpp。其中位于每个注释“/ ERROR *found*”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:You are fight. 注意:只修改注释“/ERROR *found*”的下一行语句,不要改动程序中的其他内容。 / proj1.cpp #include iostream using namespace std; class MyClass public: MyClass(int x):number(x) / ERROR *found* MyClass(int x) / ERROR *found* void Judge(MyClass private: in
14、t number; ; void Judge(MyClass else cout “Sorry“ endl; int main() / ERROR *found* MyClass object; Judge(object); return 0; (分数:30.00)_正确答案:()解析:(1)MyClass() (2)friend void Judge(MyClass 要想使屏幕输出语句:You are right.,就必须使 obj.number 的值为 10,因此要使用构造函数语句MyClass object(10);。二、简单应用题(总题数:1,分数:30.00)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.h class Shape public: virtual double perimeter() const return 0; /返回形状的周长 virtual double area() const return 0; /返回形状的面积 virtual const char * name() const return “抽象图形“; /返回形状的名称 ; class Point /表示平面坐标系中点的类 double x; double y; public: / *found* Point (double x0, double
17、 y0): _ /用 x0、y0 初始化数据成员 x、y double getX() const return x; double getY() const return y; ; class Triangle: public Shape / *found* _; /定义 3 个私有数据成员 public: Triangle (Point p1, Point p2, Point p3): point1 (p1), point2 (p2), point3 (p3) double perimeter () const; double area() const; const char * name(
18、) const return “三角形“; ; /shape.cpp #include “shape.h“ #include cmath double 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* _; d
19、ouble Triangle:area() const double s = perimeter()/2.0; return sqrt (s * (s - length (point1, point2) * (s - length (point2, point3) * (s - length (point3, point1); /proj2.cpp #include “shape.h“ #include iostream using namespace std; / *found* _ /show 函数的函数头(函数体以前的部分) cout “此图形是一个“ shape.name() “,周长
20、=“ 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)_正确答案:()解析:(1)x(x0),y(y0) (2)Point point1,point2,point3 (3)return length(point1,point2)+length(point1,point3)+length(point2,point3) (4)void show
21、(Shape class Person char name20; int age; char * 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); proj
22、3.cpp #include iostream #include string #include “proj3.h“ using namespace std; 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) deleteaddress; / *333* / *666* void Person:info_display()
23、cout name “/t“ age “/t“; if(address!=NULL) cout address endl; cout endl; Person:Person() if(address!=NULL) delete address; void main() Person p1; p1.name_change(“Zhang“); p1.age_change(20); p1.address_change(“Tsinghua University“); p1.address_change(“Tsinghua“); p1.info_display(); writeToFile(“ “);
24、(分数: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 中。