1、二级 C+-60及答案解析(总分:100.00,做题时间:90 分钟)一、B基本操作题/B(总题数:1,分数:30.00)1.请使用 VC6或使用答题菜单打开考生文件夹 proj1下的工程 proj1,此工程中含有一个源程序文件proj1.cpp。其中每个注释“/ERROR *found*”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为: The perimeter is 62.8 The area is 314 注意:只修改注释“/ERROR *found*”的下一行语句,不要改动程序中的其他内容。 / proj1.cpp #include iostream using name
2、space std; const double PI=3.14; class Circle public: / ERROR * found* Circle(int r) radius =r; void Display(); private: const int radius; ; /ERROR * found* void Display() cout “The perimeter is“ 2* PI * radius endl; cout “The area is“ PI* radius* radius endl; int main() Circle c(10); /ERROR * found
3、* c:Display(); return 0; (分数:30.00)_二、B简单应用题/B(总题数:1,分数:30.00)2.请使用 VC6或使用答题菜单打开考生文件夹 proj2下的工程 proj2,此工程中含有一个源程序文件proj2.cpp,其中定义了 Sort类和 InsertSort类。Sort 是一个表示排序算法的抽象类,成员函数 mySort为各种排序算法定义了统一的接口,成员函数 swap实现了两个整数的交换操作。InsertSort 是 Sort的派生类,它重新定义了基类中的成员函数 mySort,具体实现了简单插入排序法。本程序的正确输出结果应为: Before sort
4、ing a= 5,1,7,3,1,6,9,4,2,8,6, After sorting a= 1,1,2,3,4,5,6,6,7,8,9, 请首先阅读程序,分析输出结果,然后根据以下要求在横线处填写适当的代码并删除横线,以实现上述功能。 (1)将 Sort类的成员函数 swap补充完整,实现两个整数的交换操作; (2)将 InsertSort类的构造函数补充完整; (3)将 InsertSort类的成员函数 mySort补充完整,实现简单插入排序法(在交换数据时,请使用基类的成员函数 swap)。 注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“/*found *”
5、。 /proj2.cpp #include iostream using namespace std; class Sort public: Sort (int* a0, int n0): a (a0), n(n0) virtual void mySort() = 0; static swap(int /* found* _; y = tmp; protected: int* a; int n; ; class InsertSort : public Sort public: InsertSort(int* a0, int n0) /* found* :_ virtual void mySor
6、t() for(int i =1; in; +i) for(int j = i; j0; -j) if(aj aj-i) /* found* _; else /* found* _; ; void fun (Sort void print (int * a, int n) for(int i = 0; in; +i) cout ai “,“; cout endl; int main(int argc, char * argv) int a =5, 1, 7, 3, 1, 6, 9, 4, 2, 8, 6; cout “Before sorting a = /n“; print(a, 11);
7、InsertSort bs(a, 11); fun(bs); cout “After sorting a = /n“; print(a, ii); return 0; (分数:30.00)_三、B综合应用题/B(总题数:1,分数:40.00)3.请使用 VC6或使用答题菜单打开考生文件夹 proj3下的工程文件 proj3。本题创建一个小型字符串类,字符串长度不超过 100。程序文件包括 proj3.h、proj3.cpp、writeToFile.obj。补充完成 proj3.h,重载复合赋值运算符+=。 要求: 补充编制的内容写在“/*333*”与“/*666*”之间,不得修改程序的其他部分
8、。 注意:程序最后将结果输出到文件out.dat中。输出函数 writeToFile已经编译为 obj文件,并且在本程序中调用。 /proj3.h #include iostream #include iomanip using namespace std; class MiniString public: friendostream return output; friend istream /用于输入的临时数组 temp0 = /0; input setw(100 ) temp; int inLen = strlen(temp); /输入字符串长度 if( inLen!= 0) s.len
9、gth = inLen; /赋长度 if( s.sPtr! = 0) delete s.sPtr; s.sPtr = new char s.length + 1; strcpy( s.sPtr, temp ); /如果 s不是空指针,则复制内容 else s.sPtr 0 = /0; /如果 s是空指针,则为空字符串 return input; MiniString ( const char * s=“):length ( s!= 0 )?strlen(s) : 0 ) setString(s ); MiniString() delete sPtr; /析构函数 /* 333* /+=运算符重
10、载 /* 666* private: int length; /字符串长度 char * sPtr; /指向字符串起始位置 void setString( const char * string2 ) /辅助函数 sPtr = new char length + 1; /分配内存 if ( string2 ! = 0 ) strcpy( sPtr, string2 ); /如果 string2不是空指针,则复制内容 else smtr 0 = /0; /如果 string2是空指针,则为空字符串 ; /proj3.cpp #include iostream #include iomanip u
11、sing namespace std; #include “proj3.h“ int main () MiniString str1 (“World“), str2 (“Hello“) ; void writeToFile (char * ) ; str2 + = str1; /使用重载的+=运算符 cout str2 “/n“; writeToFile (“); return 0; (分数:40.00)_二级 C+-60答案解析(总分:100.00,做题时间:90 分钟)一、B基本操作题/B(总题数:1,分数:30.00)1.请使用 VC6或使用答题菜单打开考生文件夹 proj1下的工程 p
12、roj1,此工程中含有一个源程序文件proj1.cpp。其中每个注释“/ERROR *found*”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为: The perimeter is 62.8 The area is 314 注意:只修改注释“/ERROR *found*”的下一行语句,不要改动程序中的其他内容。 / proj1.cpp #include iostream using namespace std; const double PI=3.14; class Circle public: / ERROR * found* Circle(int r) radius =r;
13、void Display(); private: const int radius; ; /ERROR * found* void Display() cout “The perimeter is“ 2* PI * radius endl; cout “The area is“ PI* radius* radius endl; int main() Circle c(10); /ERROR * found* c:Display(); return 0; (分数:30.00)_正确答案:(1)Circle(int r):radius(r) (2)void Circle:Display() (3)
14、c.Display();)解析:考点 本题考查 Circle类,其中涉及构造函数、常变量私有成员和成员函数。 解析 (1)主要考查考生对构造函数的掌握,常变量私有成员只能通过成员列表进行初始化。 (2)主要考查考生对成员函数的掌握,在定义类的成员函数时要加上类名和作用域符。 (3)主要考查考生对成员函数调用的掌握,调用成员函数时应使用标识符“.”。二、B简单应用题/B(总题数:1,分数:30.00)2.请使用 VC6或使用答题菜单打开考生文件夹 proj2下的工程 proj2,此工程中含有一个源程序文件proj2.cpp,其中定义了 Sort类和 InsertSort类。Sort 是一个表示排
15、序算法的抽象类,成员函数 mySort为各种排序算法定义了统一的接口,成员函数 swap实现了两个整数的交换操作。InsertSort 是 Sort的派生类,它重新定义了基类中的成员函数 mySort,具体实现了简单插入排序法。本程序的正确输出结果应为: Before sorting a= 5,1,7,3,1,6,9,4,2,8,6, After sorting a= 1,1,2,3,4,5,6,6,7,8,9, 请首先阅读程序,分析输出结果,然后根据以下要求在横线处填写适当的代码并删除横线,以实现上述功能。 (1)将 Sort类的成员函数 swap补充完整,实现两个整数的交换操作; (2)将
16、 InsertSort类的构造函数补充完整; (3)将 InsertSort类的成员函数 mySort补充完整,实现简单插入排序法(在交换数据时,请使用基类的成员函数 swap)。 注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“/*found *”。 /proj2.cpp #include iostream using namespace std; class Sort public: Sort (int* a0, int n0): a (a0), n(n0) virtual void mySort() = 0; static swap(int /* found*
17、 _; y = tmp; protected: int* a; int n; ; class InsertSort : public Sort public: InsertSort(int* a0, int n0) /* found* :_ virtual void mySort() for(int i =1; in; +i) for(int j = i; j0; -j) if(aj aj-i) /* found* _; else /* found* _; ; void fun (Sort void print (int * a, int n) for(int i = 0; in; +i) c
18、out ai “,“; cout endl; int main(int argc, char * argv) int a =5, 1, 7, 3, 1, 6, 9, 4, 2, 8, 6; cout “Before sorting a = /n“; print(a, 11); InsertSort bs(a, 11); fun(bs); cout “After sorting a = /n“; print(a, ii); return 0; (分数:30.00)_正确答案:(1)x=y (2)Sort(a0, n0) (3)swap(aj, aj-1) (4)break)解析:考点 本题考查
19、Sort类及其派生类 InsertSort,其中涉及动态数组、构造函数和纯虚函数。 解析 (1)主要考查考生对成员函数的掌握,题目要求将 Sort类的成员函数 swap补充完整,实现两个整数的交换操作,因此这里是个交换操作,程序利用中问变量 tmp交换 x和 y的值。 (2)主要考查考生对构造函数的掌握,派生类的构造函数使用成员列表初始化基类。 (3)主要考查考生对成员函数调用的掌握,题目提示:在交换数据时,请使用基类的成员函数 swap。因此这里可以直接调用 swap函数交换 aj和aj-1的值。 (4)主要考查考生对成员函数的掌握,当条件 ajaj-1不满足时,说明顺序没问题不需要交换,使
20、用 break语句跳出本次循环。三、B综合应用题/B(总题数:1,分数:40.00)3.请使用 VC6或使用答题菜单打开考生文件夹 proj3下的工程文件 proj3。本题创建一个小型字符串类,字符串长度不超过 100。程序文件包括 proj3.h、proj3.cpp、writeToFile.obj。补充完成 proj3.h,重载复合赋值运算符+=。 要求: 补充编制的内容写在“/*333*”与“/*666*”之间,不得修改程序的其他部分。 注意:程序最后将结果输出到文件out.dat中。输出函数 writeToFile已经编译为 obj文件,并且在本程序中调用。 /proj3.h #incl
21、ude iostream #include iomanip using namespace std; class MiniString public: friendostream return output; friend istream /用于输入的临时数组 temp0 = /0; input setw(100 ) temp; int inLen = strlen(temp); /输入字符串长度 if( inLen!= 0) s.length = inLen; /赋长度 if( s.sPtr! = 0) delete s.sPtr; s.sPtr = new char s.length +
22、1; strcpy( s.sPtr, temp ); /如果 s不是空指针,则复制内容 else s.sPtr 0 = /0; /如果 s是空指针,则为空字符串 return input; MiniString ( const char * s=“):length ( s!= 0 )?strlen(s) : 0 ) setString(s ); MiniString() delete sPtr; /析构函数 /* 333* /+=运算符重载 /* 666* private: int length; /字符串长度 char * sPtr; /指向字符串起始位置 void setString( c
23、onst char * string2 ) /辅助函数 sPtr = new char length + 1; /分配内存 if ( string2 ! = 0 ) strcpy( sPtr, string2 ); /如果 string2不是空指针,则复制内容 else smtr 0 = /0; /如果 string2是空指针,则为空字符串 ; /proj3.cpp #include iostream #include iomanip using namespace std; #include “proj3.h“ int main () MiniString str1 (“World“), s
24、tr2 (“Hello“) ; void writeToFile (char * ) ; str2 + = str1; /使用重载的+=运算符 cout str2 “/n“; writeToFile (“); return 0; (分数:40.00)_正确答案:(MiniString /给 pt动态分配 length+1大小的空间 strcpy(pt, sPtr); /把字符串 sptr复制到 pt中 int blength=length; /把 length赋值给 blength length+=s.length; /把对象 s中 length加到 length中 deletesPtr; /
25、删除字符串 SPtr sPtr=new charlength+1; /给 sptr分配 length+1大小的内存空间 strcpy(sPtr, pt); /把 pt复制到 Sptr中 deletept; /删除字符串 pt for(int i=0; ilength; +i) /遍 s中的 sptr数组,并且把字符拷到 sptr 中 sPtrblength+i=s.sPtr i; return*this; /返回 this对象 )解析:考点 本题考查 MinString类,其中涉及构造函数、运算符重载、动态数组和析构函数。 解析 主要考查考生对运算符重载的掌握,因为有动态数组,所以要使用 new语句来重新分配空间。