国家二级C++机试(操作题)模拟试卷660
基本操作题
1.请打开考生文件夹下的解决方案文件proj1,此工程中包含程序文件main.cpp,其中有ElectricFan(“电风扇”)类和主函数main的定义。程序中位于每个“//ERROR *******found*******”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
品牌:清风牌,电源:关,风速:0
品牌:清风牌,电源:开,风速:3
品牌:清风牌,电源:关,风速:0
注意:只修改每个“//ERROR *******found*******”下的那一行,不要改动程序中的其他内容。
#include<iostream>
using namespace std;
clasS ElectricFan{//“电扇”类
char*brand;
int intensity; //风速:0一关机,1一弱,2一中,3一强
public:
ElectricFan(const char*the_brand):intensity(0){
brand=new char[strlen(thebrand)+1];
strcpy(brand,the_rand);
}
~ElectricFan(){delete[ ]brand;}
//ERROR *******found*******
const char}theBrand()const(return*brand;}//返回电扇品牌
int theIntensity()const(return intensity;}
//返回风速
bool isOn()const{return intensity>0;}
//返回电源开关状态
//ERROR *******found*******
void turnOff(){intensity=1;}//关风扇
void setIntensity(int inten){
//开电扇并设置风速
//ERROR *******found*******
if(intensity>=1&&intensity<=3)
intensity=inten ;
}
void show(){
cout<<”品牌:”<<theBrand()<<”牌”
<<”,电源:”<<(isOn()?”开”:”关”)
<<”,风速:”<<theIntensi.ty()<<endl;
}
};
int main(){
ElectricFan fan(”清风”);
fan.show();
fan.setIntensity(3);
fan.show();
fan.turnOff();
fan.show();
return 0;
}
(1)const char * theBrand()const{return brand;} //返回电扇品牌
(2)void turnOff(){intensity =0;}//关电扇
(3)if (inten >=1 && inten <=3) intensity =inten;
解析:(1)主要考查考生对指针的掌握情况,因为brand是一个动态指针,*brand表示字符串的首个字符,brand表示动态数组,这里要返回动态数组存储的品牌名称。
(2)主要考查考生对成员函数的掌握情况,根据题目中类的定义中私有成员的定义:int intensity; //风速:0-关机,1-弱,2-中,3-强,可知本函数要关电扇,因此在这里intensity =0;。
(3)主要考查考生对成员函数的掌握,根据题目中函数的注释:开电扇并设置风速。可知if语句里要判断的应该是形参inten而不是intensity。
简单应用题
2.请打开考生文件夹下的解决方案文件proj2,该工程中包含一个程序文件main.cpp,其中有类AutoMobile(“汽车”)及其派生类Car(“小轿车”)、Truck(“卡车”)的定义,还有主函数main的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
车牌号:冀ABC1234 品牌:ForLand 类别: 卡车当前档位:0最大载重量:12
车牌号:冀ABC1234 品牌:ForLand 类别:卡车当前档位:2最大载重量:12
车牌号:沪XYZ5678品牌:QQ 类别:小轿车当前档位:0座位数:5
车牌号:沪XYZ5678品牌:QQ 类别:小轿车当前档位:-1座位数:5
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//*******found*******”。
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
class AutoMobile{//“汽车”类
char*brand; //汽车品牌
char*number; //车牌号
int speed;//档位:1、2、3、4、5,空档:0。倒档:-1
public:
AutoMobile(const char * the brand,const char*the number):speed(0){
brand=new char[strlen(the brand)+1 ];
//*******found*******
________________;
//*******found*******
________________;
strcpy(number,the number);
}
~AutoMobile (){delete[ ] brand;delete[ ]number;}
const char * theBrand()const {return brand;}//返回品牌名称
const char * theNumber() const{return number;) //返回车牌号
int currentSpeed()const{return speed;} //返回当前档位
void changeGearTo(int the speed){//换到指定档位
if(speed>=-1&&speed<=5)
speed=the_speed;
}
virtual const char * category()const:0; //类别:卡车、小轿车等
virtual void show()const{
cout<<”车牌号:”<<theNumber()
//*******found*******
<<”品牌:”。“________
<
本文档预览:3600字符,共6753字符,源文件无水印,下载后包含无答案版和有答案版,查看完整word版点下载