1、二级 C+机试-36 及答案解析(总分:100.00,做题时间:90 分钟)一、1改错题(总题数:1,分数:33.00)1.使用 VC6 打开考生文件夹下的工程 test15_1,此工程包含一个源程序文件 test15_1.cpp,但该程序运行有问题,请改正程序中的错误,使该程序的输出结果如下:My object has member 7源程序文件 test15_1.cpp 清单如下:#includeiostream.hclass MyClasspublic:MyClass(int mem)member=mem;MyClass()int GetAge()const return member;
2、private:int member;/*+*+* found */void main()int mem=7;/* found */MyClass myObj=MakeObject(mem);cout“My object has member“myObj-GetAge()endl;/* found */delete;MyClass *MakeObject(int mem)MyClass *pMyClass=new MyClass(mem);return pMyClass;(分数:33.00)_二、2简单应用题(总题数:1,分数:33.00)2.请编写一个函数 char MaxCharacmr(
3、char *str),该函数返回参数 str 所指向的字符串中具有最大ASCII 码的那个字符(如字符串“world”中字符w具有最大的 ASCII 码)。当 str 所指向的字符串为空时,则返回空字符 0x0 或/0。输出结果如下:Good Morning!Max char:r注意:部分源程序已存在文件 test15_2.cpp 中。请勿修改主函数 main 和其他函数中的任何内容,仅在函数 MaxCharacter 的花括号中填写若干语句。文件 test15_2.cpp 的内容如下:#includeiostream.h#includestring.hchar MaxCharacter(ch
4、ar *str);void main()char str100;strcpy(str,“Good Morning!“);char maxc=MaxCharacter(str);coutstrendl;cout“Max char:“maxcendl;char MaxCharacter(char*str)(分数:33.00)_三、3综合应用题(总题数:1,分数:34.00)3.使用 VC6 打开考生文件夹下的工程 test15_3。此工程包含一个 test15_3.cpp,其中定义了类 Time 和Timex,Timex 公有继承 Time,但定义并不完整。请按要求完成下列操作,将程序补充完整。(
5、1)完成类 Time 构造函数的定义,将数据成员 hours 和 minutes 分别初始化为参数 new_hours 和new_minutes 的值。请在注释“/*1*”之后添加适当的语句。(2)完成类 Timex 的构造函数的定义,注意参数的传递。请在注释“/*2*”之后添加适当的语句。(3)请按时间格式“hour:minute”和“hour:minute:second”分别输出对象 time1 和 time2 所表示的时间,注意必须使用已经定义的成员函数。请在注释“/*3*”之后添加适当的语句。输出结果如下:20:3010:45:34注意:除在指定的位置添加语句外,请不要改动程序中的其他
6、语句。源程序文件 test15_3.cpp 清单如下:#includeiostream.hclass Timepublic:Time(int new_hours,int new_minutes)/ * 1 *int get_hours();int get_minutes();protected:int hours,minutes;int Time:get_hours()return hours;int Time:get_minutes()return minutes;class Timex:public Timepublic:Timex(int new_hours,int new_minute
7、s,int new_seconds);int get_seconds();protected:int seconds;/ * 2 *seconds=new_seconds;int Timex:get_seconds()return seconds;void main()Time time1(20,30);Timex time2(10,45,34);/ * 3 *(分数:34.00)_二级 C+机试-36 答案解析(总分:100.00,做题时间:90 分钟)一、1改错题(总题数:1,分数:33.00)1.使用 VC6 打开考生文件夹下的工程 test15_1,此工程包含一个源程序文件 test1
8、5_1.cpp,但该程序运行有问题,请改正程序中的错误,使该程序的输出结果如下:My object has member 7源程序文件 test15_1.cpp 清单如下:#includeiostream.hclass MyClasspublic:MyClass(int mem)member=mem;MyClass()int GetAge()const return member;private:int member;/*+*+* found */void main()int mem=7;/* found */MyClass myObj=MakeObject(mem);cout“My obje
9、ct has member“myObj-GetAge()endl;/* found */delete;MyClass *MakeObject(int mem)MyClass *pMyClass=new MyClass(mem);return pMyClass;(分数:33.00)_正确答案:(1)添加函数定义 MyClass *MakeObject(int mem);(2)错误:MyClass myObj=MakeObject(mem);正确:MyClass *myObj=MakeObject(mem);(3)错误:delete;正确:delete myObj;)解析:解析(1)主要考查考生对
10、于函数定义规则的理解,在使用前先定义,这是规定;(2)主要考查考生对于指针的掌握,因为函数返回值为指针,所以返回值必须赋值给一个指针类型的变量;(3)主要考查考生是否会使用 delete 释放空间,使用 delete 删除一个指针时,只需要直接把指针变量的名称写在后面。二、2简单应用题(总题数:1,分数:33.00)2.请编写一个函数 char MaxCharacmr(char *str),该函数返回参数 str 所指向的字符串中具有最大ASCII 码的那个字符(如字符串“world”中字符w具有最大的 ASCII 码)。当 str 所指向的字符串为空时,则返回空字符 0x0 或/0。输出结果
11、如下:Good Morning!Max char:r注意:部分源程序已存在文件 test15_2.cpp 中。请勿修改主函数 main 和其他函数中的任何内容,仅在函数 MaxCharacter 的花括号中填写若干语句。文件 test15_2.cpp 的内容如下:#includeiostream.h#includestring.hchar MaxCharacter(char *str);void main()char str100;strcpy(str,“Good Morning!“);char maxc=MaxCharacter(str);coutstrendl;cout“Max char:
12、“maxcendl;char MaxCharacter(char*str)(分数:33.00)_正确答案:(char MaxCharacter (char *str)if(str=NULL)return 0x0;char maxChar=0x0;int len=strlen(str);for(int i=0;ilen;i+)if(strimaxChar)maxChar=stri;return maxChar;)解析:解析本题考查的是考生应用 for 和 if 等基本控制语句解决实际问题的能力。注意字符的大小可以直接比较,实际上比较的就是字符的 ASCII 码的值。三、3综合应用题(总题数:1,
13、分数:34.00)3.使用 VC6 打开考生文件夹下的工程 test15_3。此工程包含一个 test15_3.cpp,其中定义了类 Time 和Timex,Timex 公有继承 Time,但定义并不完整。请按要求完成下列操作,将程序补充完整。(1)完成类 Time 构造函数的定义,将数据成员 hours 和 minutes 分别初始化为参数 new_hours 和new_minutes 的值。请在注释“/*1*”之后添加适当的语句。(2)完成类 Timex 的构造函数的定义,注意参数的传递。请在注释“/*2*”之后添加适当的语句。(3)请按时间格式“hour:minute”和“hour:mi
14、nute:second”分别输出对象 time1 和 time2 所表示的时间,注意必须使用已经定义的成员函数。请在注释“/*3*”之后添加适当的语句。输出结果如下:20:3010:45:34注意:除在指定的位置添加语句外,请不要改动程序中的其他语句。源程序文件 test15_3.cpp 清单如下:#includeiostream.hclass Timepublic:Time(int new_hours,int new_minutes)/ * 1 *int get_hours();int get_minutes();protected:int hours,minutes;int Time:ge
15、t_hours()return hours;int Time:get_minutes()return minutes;class Timex:public Timepublic:Timex(int new_hours,int new_minutes,int new_seconds);int get_seconds();protected:int seconds;/ * 2 *seconds=new_seconds;int Timex:get_seconds()return seconds;void main()Time time1(20,30);Timex time2(10,45,34);/
16、* 3 *(分数:34.00)_正确答案:(1)hours=new_hours;minutes=new_minutes;(2)Timex:Timex(int new_hours,int new_minutes,int new_seconds):Time(new_hours,new_minutes)(3)couttime1.get_hours()“:“time1.get_minutes()end1;couttime2.get_hours()“:“time2.get_minutes()“:“time2.get_seconds()endl;)解析:解析本题主要考查考生对于类和派生类构造函数的定义以及成员函数的使用,注意(2)中派生类构造函数中必须包括向基类传递参数的调用,应该使用参数列表完成这一操作。