一、程序改错题
使用VC++6.0打开考生文件夹下的源程序文件1.cpp,该程序运行时有错误,请改正错误,使得程序输出:
Hello
test
注意:不要改动main函数,不能增加或删除行,也不能更改程序的结构,错误的语句在
//******error******的下面。
试题程序:
#include
//********error********
template
voidfun(Tt)
{
std::cout<<"test"<<std::endl;< p="">
}
//********error********
template
voidfun(boolt)
{
std::cout<<(t?"Hell0":"Hi")<<std::endl;< p="">
}
intmain()
{
//********error********
boolflag=TRUE;
fun(flag);
fun((int)flag);
return0;
}
答案:(1)应改为“template”。
(2)删除“template(bool)”。
(3)应改为“boolflag=true;”。
二、简单应用题
使用VC++6.0打开考生文件夹下的源程序文件2.cpp。请完成函数fun(intx)的定义,该函数功能是判定x的所有约数,并且在函数中调用写函数WriteFile将结果输出到2.txt文件中。
例如:x=100的约数为124510202550100。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include<iostream)< p="">
#include(fstream)
#include(cmath)
usingnamespacestd;
voidWriteFile(intc)
{
ofstreamout1;
out1.open("2.txt",ios_base::binary|ios_base::app);
out1<<c<<'';’< p="">
out1.close();
}
voidfun(intx)
{
}
voidClearFile()
{
ofstreamout1;
out1.open("2.txt");
out1.close();
}
intmain()
{
ClearFile();
fun(100);
return0;
}
答案:for(inti=1:i<=x;i++)
{
if(x%i==0)//如果x被i整除,那么i为X的约数
{
writeFile(i);//将约数i存入文件
}
}
三、综合应用题
用vc++6.0打开考生文件夹下的源程序文件3.cpp,其中定义了多个类,其中定义的类并不完整,按照要求完成下列操作,将类的定义补充完整;使其输出如下:
TC2
TC3
TCl
(1)类TC2公共继承于TC1,请在注释1后添加适当的语句。
(2)print函数为虚函数,请在注释2后添加适当的语句。
(3)修改语句,使得p指定对象0bj3,请在注释3后添加
适当的语句。
(4)修改语句,利用P调用print函数,完成输出信息,在注释4后添加适当的语句。
注意:仅在函数指定位置添加语句,请勿改动主函数main与其他函数中的任何内容。
试题程序:
#include
usingnamespacestd;
classTCl
{
public:
voidprint()
{
cout<<"TCl"<<endl:< p="">
}
};
//********1********
classTC2
{
public:
//********2********
voidprint()
{
cout<<"TC2"<<endl;< p="">
}
};
classTC3:publicTC2
{
public:
voidprint()
{
cout<<"TC3"<<endl;< p="">
}
};
intmain()
{
TC20bj2;
TC30bj3;
TC1*P;
obj2.print();
obj3.print();
//********3********
p=obj3;
//********4********
P.print();
return0;
}
答案:(1)将“classTC2”补充完整为“classTC2:publicTCl”。
(2)将“voidprint()”补充完整为“virtualvoidprint()”。
(3)将“p=obj3;”修改为“p=&obj3;”。
(4)将“P.print();”修改为“p->print();”。


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