1、二级 C+-44及答案解析(总分:100.00,做题时间:90 分钟)一、B基本操作题/B(总题数:1,分数:30.00)1.请使用 VC6或使用答题菜单打开考生文件夹 proj1下的工程 proj1,此工程中包含了类 Pets(“宠物”)和主函数 main的定义。程序中位于每个“/ERROR*found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: Name: sonny Type: dog Name: John Type: dog Name: Danny Type: eat Name: John Type: dog 注意:只修改每个“/ERROR*found*”下的那一行
2、,不要改动程序中的其他内容。 #includeiostream using namespace std; enum Pets_typedog, cat, bird, fish; class Pets private: char*name; Pets_type type; public: Pets(const char*name=“sonny“, Pets type type=dog); Pets Pets(); void show()const; ; Pets:Pets(const char*name, Pets_type type) /构造函数 this-name=new charstrle
3、n (name)+1; strcpy(this-name, name); /ERROR*found* type=type; Pets:Pets()/析构函数,释放 name所指向的字符串 /ERROR*found* name=/0; Pets deletename; name=new charstrlen(s.name)+1; /ERROR*found* strcpy(this-name, name); type=s.type; return*this; void Pets:show()const cout“Name:“name“Type:“; switch(type) case dog: c
4、out“dog“; break; case cat: cout“cat“; break; case bird: cout“bird“; break; case fish: cout“fish“; break; coutendl; int main() Pets mypet1, mypet2(“John“, dog); Pets youpet(“Danny“, cat); mypet1.show(); mypet2.show(); youpet.show(); youpet=mypet2; youpet.show(); return 0; (分数:30.00)_二、B简单应用题/B(总题数:1,
5、分数:30.00)2.请使用 VC6或使用答题菜单打开考生文件夹 proj2下的工程 proj2,该工程中包含一个程序文件main.cpp,其中有类 CPolygon(“多边形”)、CRectangle(“矩形”)、CTriangle(“三角形”)的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。该程序的正确输出结果应为: 20 10 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“/*found*”。 #includeiostream using namespace std; class CPolygon public: /*found* _/纯虚函
6、数 area声明 void printarea(void) /*found* cout_endl; ; class CRectangle: public CPolygon int width; /长方形宽 int height; /长方形高 public: CRectangle(int w, int h): width(w), height(h) int area(void) return(width*height); ; class CTriangle: public CPolygon int length; /三角形一边长 int height; /该边上的高 public: CTrian
7、gle(int 1, int h): length(1), height(h) /*found* int area(void)return(_)/2;) ; int main() CRectangle rect(4, 5); CTriangle trgl(4, 5); /*found* _*ppoly1, *ppoly2; ppoly1= ppoly2= ppoly1-printarea(); ppoly2-printarea(); return 0; (分数:30.00)_三、B综合应用题/B(总题数:1,分数:40.00)3.请使用答题菜单命令或直接用 VC6打开考生文件夹下的工程 pro
8、g3,其中声明了 ValArray类,该类在内部维护一个动态分配的整型数组。ValArray 类的复制构造函数应实现对象的深层复制。请编写ValArray类的复制构造函数。在 main函数中给出了一组测试数据,此种情况下程序的输出应该是: ValArray v1=1,2,3,4,5 ValArray v2=1,2,3,4,5 要求: 补充编制的内容写在“/*333*”与“/*666*”之间,不得修改程序的其他部分。 注意:程序最后将结果输出到文件 out.dat中。输出函数 writeToFile已经编译为 boj文件,并且在本程序中调用。/ValArray.h #includeiostrea
9、m using namespace std; class ValArray int*v; int size; public: ValArray(const int*p, int n); siZe(n) v=new intsize; for(int i=0; isize; i+) vi=pi; VaiArray(const ValArray ValArray() deletev;) void print(ostream for(int i=0; isize-1; i+) outvi “,“; outvsize-1; void setArray(int i, int val) vi=val; ;
10、void writeToFile(const char*); /main.cpp #include“ValArray.h“ ValArray:ValArray(const ValArray ValArray vl(a, 5); cout“ValArray v1=“; v1.print(cout); coutendl; ValArray v2(v1); cout“ValArray v2=“; v2.print(cout); coutendl; writeToFile(“); return 0; (分数:40.00)_二级 C+-44答案解析(总分:100.00,做题时间:90 分钟)一、B基本操
11、作题/B(总题数:1,分数:30.00)1.请使用 VC6或使用答题菜单打开考生文件夹 proj1下的工程 proj1,此工程中包含了类 Pets(“宠物”)和主函数 main的定义。程序中位于每个“/ERROR*found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: Name: sonny Type: dog Name: John Type: dog Name: Danny Type: eat Name: John Type: dog 注意:只修改每个“/ERROR*found*”下的那一行,不要改动程序中的其他内容。 #includeiostream using nam
12、espace std; enum Pets_typedog, cat, bird, fish; class Pets private: char*name; Pets_type type; public: Pets(const char*name=“sonny“, Pets type type=dog); Pets Pets(); void show()const; ; Pets:Pets(const char*name, Pets_type type) /构造函数 this-name=new charstrlen (name)+1; strcpy(this-name, name); /ERR
13、OR*found* type=type; Pets:Pets()/析构函数,释放 name所指向的字符串 /ERROR*found* name=/0; Pets deletename; name=new charstrlen(s.name)+1; /ERROR*found* strcpy(this-name, name); type=s.type; return*this; void Pets:show()const cout“Name:“name“Type:“; switch(type) case dog: cout“dog“; break; case cat: cout“cat“; bre
14、ak; case bird: cout“bird“; break; case fish: cout“fish“; break; coutendl; int main() Pets mypet1, mypet2(“John“, dog); Pets youpet(“Danny“, cat); mypet1.show(); mypet2.show(); youpet.show(); youpet=mypet2; youpet.show(); return 0; (分数:30.00)_正确答案:(1)this-type=type; (2)deletename; (3)strcpy(this-name
15、, s.name);)解析:考点 主要考查的是 Pets类,其中涉及 enum类型、动态数组、构造函数、运算符重载、析构函数和 const函数。本题程序很长,涉及的函数类型较多,但考查的内容较简单,只要注意细节便可答对此题。 解析 (1)主要考查考生对构造函数的掌握情况,因为形参名和类的私有成员名称都是 type,为了避免混淆,所以规定类的私有成员使用 this指针调用,即:this-type=type;。 (2)主要考查考生对析构函数的掌握情况,题目中要求,释放 name所指向的字符串。要释放 name指针用 delete语句,即deletename;。 (3)主要考查考生对 strcpy函
16、数的掌握情况,strcpy 函数的形参为两个字符串,而name为指向字符串的指针,因此使用语句:strcpy(this-name, s.name);。 主要考查考生对构造函数、析构函数和 strcpy()函数的掌握,构造函数中当类的私有成员和形参名称相同时,为了区别类的成员要调用 this指针来区分。析构函数必须要用 delete语句释放指针。二、B简单应用题/B(总题数:1,分数:30.00)2.请使用 VC6或使用答题菜单打开考生文件夹 proj2下的工程 proj2,该工程中包含一个程序文件main.cpp,其中有类 CPolygon(“多边形”)、CRectangle(“矩形”)、CT
17、riangle(“三角形”)的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。该程序的正确输出结果应为: 20 10 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“/*found*”。 #includeiostream using namespace std; class CPolygon public: /*found* _/纯虚函数 area声明 void printarea(void) /*found* cout_endl; ; class CRectangle: public CPolygon int width; /长方形宽 int hei
18、ght; /长方形高 public: CRectangle(int w, int h): width(w), height(h) int area(void) return(width*height); ; class CTriangle: public CPolygon int length; /三角形一边长 int height; /该边上的高 public: CTriangle(int 1, int h): length(1), height(h) /*found* int area(void)return(_)/2;) ; int main() CRectangle rect(4, 5
19、); CTriangle trgl(4, 5); /*found* _*ppoly1, *ppoly2; ppoly1= ppoly2= ppoly1-printarea(); ppoly2-printarea(); return 0; (分数:30.00)_正确答案:(1)virtual int area(void)=0; (2)area() (3)length*height (4)CPolygon)解析:考点 主要考查的是 CPolygon类及其派生类 CRectangle类和 CTriangle类,其中涉及纯虚函数和构造函数。在定义纯虚函数时要参考在派生类中的同名函数的定义,要特别注意函
20、数的返回类型和形参。解析 (1)主要考查考生对纯虚函数的掌握,在定义纯虚函数时要看在派生类中函数的定义:int area(void)。由此可知纯虚函数应该为:virtual int area(void)=0;。 (2)主要考查考生对纯虚函数的掌握情况,由 void printarea(void)可知,该函数要打印面积,因此在此要调用纯虚函数 area,即contarea()。 (3)主要考查考生对数学公式的掌握,该函数要返回三角形面积,三角形的面积公式为长乘以该边上的高除以 2,即 return(length*height)/2;。 (4)主要考查考生对抽象类的掌握情况,根据程序段: ppol
21、y1= ppoly2= 可知指针 ppoly1指向 CRectangle类,而指针 ppoly2指向CTriangle类,因此在这里只能填这两种类的基类 CPolygon类。 主要考查考生对纯虚函数、抽象基类及成员函数的掌握情况,常用的数学公式,如三角形面积是长乘以高除以 2,矩形面积是长乘以高等。三、B综合应用题/B(总题数:1,分数:40.00)3.请使用答题菜单命令或直接用 VC6打开考生文件夹下的工程 prog3,其中声明了 ValArray类,该类在内部维护一个动态分配的整型数组。ValArray 类的复制构造函数应实现对象的深层复制。请编写ValArray类的复制构造函数。在 ma
22、in函数中给出了一组测试数据,此种情况下程序的输出应该是: ValArray v1=1,2,3,4,5 ValArray v2=1,2,3,4,5 要求: 补充编制的内容写在“/*333*”与“/*666*”之间,不得修改程序的其他部分。 注意:程序最后将结果输出到文件 out.dat中。输出函数 writeToFile已经编译为 boj文件,并且在本程序中调用。/ValArray.h #includeiostream using namespace std; class ValArray int*v; int size; public: ValArray(const int*p, int n
23、); siZe(n) v=new intsize; for(int i=0; isize; i+) vi=pi; VaiArray(const ValArray ValArray() deletev;) void print(ostream for(int i=0; isize-1; i+) outvi “,“; outvsize-1; void setArray(int i, int val) vi=val; ; void writeToFile(const char*); /main.cpp #include“ValArray.h“ ValArray:ValArray(const ValA
24、rray ValArray vl(a, 5); cout“ValArray v1=“; v1.print(cout); coutendl; ValArray v2(v1); cout“ValArray v2=“; v2.print(cout); coutendl; writeToFile(“); return 0; (分数:40.00)_正确答案:(size=other.size; v=new intsize; for(int i=0; isize; i+) setArray(i, other. vi);)解析:考点 主要考查的是 ValArray类,其中涉及动态数组、构造函数、复制构造函数、析构函数和const函数。注意动态数组的复制构造函数要先给动态数组分配空间,再逐个元素复制。 解析 主要考查考生对复制构造函数的掌握,ValArray 类的复制构造函数应实现对象的深层复制。由 ValArray类的构造函数: ValArray(const int* p, int n) : size (n) v=new intsize; /给 v分配大小为 Size的空间 for (int i =0; isize; i+) /遍历 p vi=pi; /把 pi赋值给 vi 可知类中 v是动态数组,size 表示数组长度,因此要先给 v分配空间为 size,再逐个元素复制以达到对象的深层复制。