一、程序改错题
使用VC++6.0打开考生文件夹下的源程序文件1.cpp,该程序运行时有错误,请改正其中的错误,使程序正确运行,并且使程序输出的结果为
0K
注意:错误的语句在//******error******的下面,修改该语句即可。
试题程序:
#include
classTC
{
public:
TC()
{
a=b=0:
}
private:
inta,b;
};
classTCl:publicTC
{
public:
TC1()
{
}
//******error******
virtualvoidfunc();
};
classTC2:publicTCl
{
public:
TC2()
{
a=0:
b=0:
}
voidfunc()
{
//******error******
cout<<"OK"<endl;< p="">
}
private:
inta;
intb;
};
voidmain()
{
TC20bj;
//******error******
TCl*p=(TCl*)obj;
p->func();
}
答案:(1)应改为“virtualvoidfunc()=0;”。
(2)应改为“cout<<"0K"<(endl;”。
(3)应改为“TCl*p=(TCl*)&obj;”。
二、简单应用题
使用VC++6.0打开考生文件夹下的源程序文件2.epp。完成空出函数fun(char*S,char*t)部分。函数fun(char*s,char*t)的功能是将在字符串S中下标为偶数位置上的字符,紧随其后重复出现一次,放在一个新串t中,t中字符按原字符串中字符的顺序排列。
例如:当s中的字符串为“ABCDEF”时,t中的字符串应为“AACCEE”。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include
voidfun(char*s,char*t)
{
}
voidmain()
{
chars[100],t[100];
cout<<"Pleaseenterstrings:"<<endl;< p="">
tin.getline(s,100);
fun(s,t);
cout<<"Theresultis:"<<t<<endl;< p="">
return;
}
答案:voidfun(char*s,char*t)
{
inti;
for(i=0;s[i]!==0;i++)
{
if(i%2==0)
{
t[i]=s[i];
t[i+2]='\0';
}
}
}
三、综合应用题
使用VC++6.0打开考生文件夹下的源程序文件3.cpp,其中定义了用于表示个人基本信息的类PInfo,但类PInfo的定义并不完整。请按要求完成下列操作,将类Pin-fo的定义补充完成:
(1)定义私有数据成员bloodType用于表示血型,血型为char型的数据。请在注释1之后添加适当的语句。
(2)完成构造函数的定义,要求具有默认值,默认值为
身高175,体重70,血型A。请在注释2之后添加适当的语句。
(3)完成类PInfo外成员函数SetInfo的定义。请在注
释3之后添加适当的语句。
(4)在主函数main中调用成员函数Setlnfo,把对象d2的3个私有数据成员分别设定为身高170,体重64,血型B。
请在注释4之后添加适当的语句。
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
试题程序:
#include
classPlnfo
{
private:
intheight;
intweight;
//********1********
public:
//********2********
:height(ht),weight(wt),bloodType(bt){};
Plnfo(Plnfo&h1):height(h1.height),
weight(h1.weight),bloodType(h1.bloodType){}
intGetHeight()
{
returnheight;
}
intGetWeight()
{
returnweight;
}
intGetBloodType()
{
returnbloodType;
}
voidSetlnfo(intht,intwt,charbt);
voidDisplay();
};
//********3********
{
height=ht;
weight=wt;
bloodType=bt;
}
voidPlnfo::Display()
{
cout<<"Humanlnf0:";
cout<<height<<"cm,"<<weight<<"kg,blood-< p="">
Type"<<bloodtype<<endl;< p="">
}
voidmain()
{
PInfoh1(169,61,'A');
Plnfoh2;
Plnfoh3(h1);
Plnfoh4(h2);
//********4********
h1.Display();
h2.Display();
h3.Display():
h4.Display();
}
答案:(1)应添加“charbloodType;”。
(2)应添加“Plnfo(intht=175,intwt=70,charbt='A')”。
(3)应添加“voidPlnf0::Setlnfo(intht,intwt,charbt)”。
(4)应添加“h2.SetInfo(170,64,'B');”。


热门课程培训
热门培训机构
