国家二级C++机试(操作题)模拟试卷365
基本操作题
1.请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程proj1,该工程中包含程序文件main.cpp,其中有关TVSet(“电视机”)和主函数main的定义。程序中位于每个“//ERROR**********found**********”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是:
规格:29英寸,电源:开,频道:5,音量:18
规格:29英寸,电源:关,频道:一1,音量:一1
注意:只修改每个“//ERROR****found****”下的那一行,不要改动程序中的其他内容。
#include
using namespace std;
class TVSet{ //“电视机”类
const int Size;
int channel; //频道
int volume; //音量
bool on; //电源开关:true表示开,false表示关
public:
//ERROR*********found*********
TVSet(int size){
this一>size(size);
channel=0;
volume=15;
on=false;
}
int getSize()const{return size;)
//返回电视机规格
bool isOn()const{return on;) //
返回电源开关状态
//返回当前音量,关机情况下返回一1
int getVolume()const{return isOn
()?volume:一1;)
//返回当前频道,关机情况下返回一1
int getChannel()const{return isOn
()?channel:一1;)
//ERROR*********found*********
void turnOnOff()const//将电源在
“开”和“关”之间转换
{on=!on;)
void setChannelTo(int chan){ //设
置频道(关机情况下无效)
if(isOn()&&chan>=0&&chan<=
99)
channel=chan;
}
void setVolumeTo(int vol){ //设置
音量(关机情况下无效)
if(isOn()&&vol>=0&&vol<=30)
volume=vol;
}
void show state(){
//ERROR*********found*********
COUt<<“规格:”<<getSize()<<”英寸”
《”,电源:”《(isOn()?\\
(1)TVSet(int size):size(size){
(2)void turnOnOff()
(3)cout<<”规格:”<<getSize()<<”英寸”
解析:(1)主要考查考生对构造函数的掌握,因为size是常变量,所以只能用成员初始化列表来初始size,即TVSet(intsize):size(size){。
(2)主要考查考生对const函数的掌握,在tumOnOff函数中,有语句:on=!on;,使得on的值发生改变,因此该函数不能使用const。
(3)主要考查考生对输出语句的掌握,下一条语句:<<”,电源:”<<(isOn()?”开”:”关”),说明输出语句还没结束,因此不能用“;”。
简单应用题
2.请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,其中定义了Component类、Composite类和Leaf类。Component是抽象基类,Composite和Leaf是Component的公有派生类。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
Leaf Node
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include
using namespace std;
class Component{
public:
//声明纯虚函数print()
//********** found**********
};
class Composite:public Component{
public:
//**********found**********
void setChiid(_______)
{
m chiId=child;
}
virtual void print()const
{
m child->print();
}
private:
Component*m—child;
};
class Leaf:public Component{
public:
virtual void print()const
{
//**********found**********
}
};
int main()
{
Leaf node;
Composite comp;
comp.setChild(&node);
Component*P=&comp;
P->print();
return 0;
}
(1)virtual void print()const=0;
(2)Component*child
(3)cout<<”Leaf Node”<<endl;
解析:(1)主要考查考生对纯虚函数的掌握,题目要求声明纯虚函数print()。在其派生类中print()函数的定义为virtualvoid print()const,由此可知纯虚函数为virtual void print()const=0.
(2)主要考查考生对成员函数的掌握,题目要求填写函数void setChild的形参,由setChild的函数体可知形参为child,再看类的私有成员m_child的定义:Component*m_child;。由此可知形参为:Component*child。
(3)主要考查考生对纯虚函数的掌握,先看
本文档预览:3600字符,共9994字符,源文件无水印,下载后包含无答案版和有答案版,查看完整word版点下载