首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
请打开考生文件夹下的解决方案文件proj2,该工程中包含一个程序文件main.cpp,其中有类AutoMobile(“汽车”)及其派生类Car(“小轿车”)、Truck(“卡车”)的定义,还有主函数main的定义。请在横线处填写适当的代码并删除横线,以实现
请打开考生文件夹下的解决方案文件proj2,该工程中包含一个程序文件main.cpp,其中有类AutoMobile(“汽车”)及其派生类Car(“小轿车”)、Truck(“卡车”)的定义,还有主函数main的定义。请在横线处填写适当的代码并删除横线,以实现
admin
2020-05-14
26
问题
请打开考生文件夹下的解决方案文件proj2,该工程中包含一个程序文件main.cpp,其中有类AutoMobile(“汽车”)及其派生类Car(“小轿车”)、Truck(“卡车”)的定义,还有主函数main的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
车牌号:冀ABCl234品牌: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,thenumber);
}
~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;//类别:卡车、小轿车等
virtualVOidshow( )const{
cout<<"车牌号:"<<theNumber( )
//*******found*******
<<"品牌:"<<_______
<<"类别:"<<category( )
<<"当前档位:"<<currentSpeed( );
}
};
class Car:public AutoMobile{
int seats;//座位数
public:
Car(const char*the_brand,const char*the_number,int the_seatS):AutoMobile(the_brand,the_number),seats(the_seats){}
int numberOfSeat( )const{return seats;}
//返回座位数
const char*category( )const{return"小轿车";}//返回汽车类别
void show( )const{
AutoMobile::show( );
cout<<"座位数:"<<numberOfSeat( )<<endl;
}
};
class Truck:public AutoMobile
{//“卡车”类
int max_load;//最大载重量
public:
Truck(const char*the_brand,const char*the_number,int the_max_load):AutoMobile(the_brand,the_number),max_load(the_max_load){}
int maxLoad( )conSt {returnmax_load;)//返回最大载重量
//*******found*******
const char*category( )
_______//返回汽车类别
void show( )const{
AutoMobile::show( );
cout<<"最大载重量:"<<maxLoad( )<<endl;
}
};
int main( ){
Truck truck("ForLand","冀ABC1234",12);
ttuck.show( );
truck.changeGearTo(2);
truck.show( );
Carcar("QQ","沪XYZ5678”,5);
car.show( );
car.changeGearTo(-1);
car.show( );
cout<<endl;
return0;
}
选项
答案
(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指针的复制过程完成该语句,先给指针number分配空间,通过new来完成:number=new char[strlen(the_number)+1];。
(3)主要考查考生对成员函数的掌握,由程序可知这里要输出的是品牌,因此调用成员函数theBrand( )来输出品牌。
(4)主要考查考生对纯虚函数的掌握,根据纯虚函数的定义:virtual const char*category( )const=0;,可知在这里要填写:const{return"卡车";}。
转载请注明原文地址:https://kaotiyun.com/show/xg8p777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
有如下程序:#include<iostream>usingnamespacestd;longfun(intn){if(n>2)return(fun(n-1)+fun(n-2));return
数据库的逻辑模型设计阶段的任务是将______转换成关系模式。
以下程序的输出结果是______。#defineNULL0#include<iostream.h>voidmain(){inta[]={1,2,3,4,5,6,7,8,9,10};int*p=
下列有关拷贝构造函数的描述中错误的是()。
下列代码段声明了3个类:classPerson();classStudent:publicPerson{};classUndergraduate:Student{};下列关于这些类之间关系的描述中,错误的是()。
如下程序声明了一个使用两个通用数据类型的模板类dataClass,其中构造函数用于初始化两个数据成员,成员函数show用于输出两个数据成员的数值。#include<iostream>usingnamespaeestd;_
重载输入流运算符>>必须使用的原型为
如果表达式y*x++中,“*”是作为成员函数重载的运算符,“++”是作为友元函数重载的运算符,采用运算符函数调用格式,该表达式还可表示为
下图所示的二叉树的先序遍历序列是【】。
某二叉树中度为2的结点有18个,则该二叉树中有______个叶子结点。
随机试题
关系表中的每一横行称为一个()。
我国将建成“四纵四横”为重点的高速铁路网,下列属于“四纵”高速铁路网的是()。
等离子弧切割的速度比氧-乙炔切割________。
A.槲寄生B.大血藤C.桑寄生D.沉香E.鸡血藤断面皮部红棕色,有数处向内嵌入木部,术部黄白色,有多数细孔状导管,射线呈放射状排列的药材是()。
()是指由有权代表国家投资的政府部门或机构,用预算内资金、各类专项建设资金投入建设项目形成的资本金。
产品方案在______研究的基础上形成。
当人们普遍感到现在的小说越来越缺乏想象力的时候,网络上的小说却用_______的想象力吸引了众多网民的眼球,网络小说可以穿越时空,让现代人与古代人对话,也可以_______一个魔幻的世界,让人的灵魂和身体相互置换。填入画横线部分最恰当的一项是(
Accordingtothepassage,thefivewell-knownplanthormonesarenotusefulincontrollingthegrowthofcropsbecause______Th
Whatisthemainsubjectofthistalk?
A、LGhasexposednewconceptTVs.B、ThemarketofTVsdecreased.C、TVwillbereplacedbycomputers.D、LGislaggingbehindint
最新回复
(
0
)