国家二级C++机试(操作题)模拟试卷632
基本操作题
1.请打开考生文件夹下的解决方案文件proj1,其中有枚举DOGCOLOR、狗类Dog和主函数main的定义。程序中位于每个“//ERROR **** found ****”下的语句行有错误,请加以改正。改正后程序的输出结果应该是:
There is a white dog named Hobo.
There is a black dog named Haha.
There is a motley dog named Hihi.
注意:只修改每个“//ERROR **** found ****”下的那一行,不要改动程序中的其他内容。
#include<iostream>
using namespace std;
//狗的颜色:黑、白、黄、褐、花、其他
enum DOGCOLOR {BLACK, WHITE,YELLOW, BROWN, PIEBALD,OTHER};
class Dog(//狗类
DOGCOLOR color;
char FIame[2 0];
static int count;
public:
Dog(char flame[],DOGCOLOR color){
strcpy(this一>name,name);
//ERROR ******* found *******
strcpy(this一>color,color);
}
DOGCOLOR getColor()const {return color;}
//ERROR ******* found *******
const char * getName()
const{ return * name;)
const char * getColorString()const{
switch(color){
case BLACK: return\\
(1)this一>color=color;
(2)const char getName()const{return *name;}
(3)Dog dogl(\\
解析:(1)主要考查考生对strcpy函数的掌握,如果看到上一条语句strcpy(this—>name,name);,就以为本条语句也要用strcpy函数来赋值,这是错误的。Strcpy函数只能复制字符串,根据类的私有成员声明可知,color是DOGCOLOR型的,这里直接使用赋值语句“=”即可。
(2)主要考查考生对函数返回值的掌握,先解读语句const char * getName()const{return * name;},要返回的是一个const的字符指针,同时函数内的值不能改变,name在类的私有成员声明中是个字符数组,* name代表字符数组而不是字符指针,问题就出来了,需要修改返回类型:const,chat getName()const{retum name;}。
(3)语法错误,定义变量时,变量之间应使用“,”分开。
简单应用题
2.请打开考生文件夹下的解决方案文件proj2,该工程中包含一个程序文件main.epp,其中有类AutoMobile(“汽车”)及其派生类Car(“小轿车”)、Truck(“卡车”)的定义,还有主函数main的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
车牌号:冀ABC1234 品牌:ForLand 类别:卡车当前档位:0最大载重量:12
车牌号:冀ABCl234 品牌:ForLand 类别:卡车当前档位:2最大载重量:12
车牌号:沪XYZ5678品牌:QQ类别:小轿车当前档位:0座位数:5
车牌号:沪XYZ5678 品牌:QQ类别:小轿车当前档位:一1座位数:5
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#inclue<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 changeGearT0(int the_speed){ //换到指定档位
if(speed>=一1 && speed<=5)
speed=the_speed;
}
virtual const char * category()const=0; //类别:卡车、小轿车等
virtual void show()const{
cout<<\\
(1)strcpy(brand,the_brand)
(2)number=new char[strlen(the_number)+1]
(3)theBrand()
(4)const{return\\
解析:(1)主要考查考生对strcpy函数的掌握情况,在上一条语句程序给brand指针分配了空间,在这里要复制字符串the_brand,即strcpy(brand,the_brand);。
(2)主要考查考生对动态分配的掌握情况,参考brand指针的复制过程完成该语句,
本文档预览:3600字符,共6224字符,源文件无水印,下载后包含无答案版和有答案版,查看完整word版点下载