国家二级C++机试(操作题)模拟试卷362
基本操作题
1.请使用VG6或使用【答题】菜单打开考生文件夹pmjl下的工程proj1,其中有枚举DOGCOLOR、狗类Dog和主函数main的定义。程序中位于每个“//ERROR****found****”下的语句行有错误,请加以改正。改正后程序的输出结果应该是:
There is a white dog named Hoho.
There is a black dog named HaIla.
There is a motley dog named Hihi.
注意:只修改每个“//ERROR****found****”下的那一行,不要改动程序中的其他内容。
#include
using namespace std;
//狗的颜色:黑、白、黄、褐、花、其他
enum DOGCOLOR{BLACK,WHTTE,YELLOW,
BROWN,PTEBALD,OTHER);
class Dog {//狗类
DOGCOLOR colot;
char name[2 O];
static int count;
public:
Dog(char name[],DOGCOLOR color){
strcpy(this一>name,name);
//ERROR**********found**********
strcpy(this一>color,color);
}
DOGCOLOR getColor()const{return
color;)
//ERROR**********found**********
COnSt char*get:Name()const{return*name;)
const char* getColorString()
const{
switch(colot){
CaSe BLACK:return”blaCk”;
case WHITE:return”white”;
CaSe YELLOW:return”yellow”;
case BROWN:return”brown”;
CaSe PIEBALD:return”piebald”;
}
return”motley”;
}
Void show()const{
cout<<”There is a”<<getColor
String()<<”dog named”<<name<<’.’
<<endl;
}
};
int main(){
//ERROR**********found**********
Dog dog1(”Hobo”,WHITE),dog2(”Haha”,BLACK);dog3(”Hihi”,OTHER);
dog1.show();
dog2.show();
dog3.show();
return 0;
}
(1)this->color=color;
(2)const char getName()const{return *name;}
(3)Dog dogl(”Hoho”,WHITE),dog2(”Haha”,BLACK),dg3(”Hihi”,OTHER);
解析:(1)主要考查考生对strcpy函数的掌握,如果看到上一条语句strcpy(this一>nallle,name);,就以为本条语句也要用strcpy函数来赋值,这是错误的。Strcpy函数只能复制字符串,根据类的私有成员声明可知,color是DOGCOLOR型的,这里直接使用赋值语句“=”即可。
(2)主要考查考生对函数返回值的掌握,先解读语句const char*getName()const{return*name;},要返回的是一个const的字符指针,同时函数内的值不能改变,name在:类的私有成员声明中是个字符数组,*name代表字符数组而不是字符指针;问题就出来了,需要修改返回类型:constchar getName()const{return*name;}。
(3)语法错误,定义变量时,变量之间应使用“,”分开。
2.请使用VC6或使用【答题】菜单打开考生文件夹pmj1下的工程pmj1,此工程中含有一个源程序文件pmj1.cpp。其中位于每个注释“//ERROR ****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:
The value is:10
注意:只修改注释“//ERROR ****found****”的下一行语句,不要改动程序中的其他内容。
//proj1.cpp
#include
using namespace std;
class Member {
//ERROR********found********
private:
Member(int val):value(val){}.
int value;
},
class MyClass{
Member m;
public:
//ERROR********found********
MyClass(int val){}
int GetValue()const{return m.
value;)
};
int main()
{
MyClass*obj=new MyClass(i0);
//ERROR********found********
下列语句输出obj指向类中的value值
cout<<”The value is:”<<obj.
GetValue()<<endl;
delete obj;
return 0;
}
(1)public:
(2)MyClass(int val):_m(val){}或MyClass(int val){_m=val}
(3)cout<<”The value is:”<GetValue()<<endl;
解析:(1)主要考查考生对私有成员和公有成员的掌握,先看改错语句的下一条语句:Member(int val):value(val){},该语句是一个构造函数,因此我们可以得出此处为公有成员,因为构造函数不可能是私有成员。
(2)主要考查构造函数,构造函数要对类的成员进行初始化,因此在这里使用成员列表初始化,即MyClass(int val):_m(val){}或MyClass(int val){_m=val}。
(3)指针调用类的成员函数时,应使用标识符“一
本文档预览:3600字符,共9755字符,源文件无水印,下载后包含无答案版和有答案版,查看完整word版点下载