首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
请打开考生文件夹下的解决方案文件proj2,其中定义了vehicle类,并派生出motorcar类和bicycle类。然后以motorcar和bicycle作为基类,再派生出motorcycle类。要求将vehicle作为虚基类,避免二义性问题。请在程序中
请打开考生文件夹下的解决方案文件proj2,其中定义了vehicle类,并派生出motorcar类和bicycle类。然后以motorcar和bicycle作为基类,再派生出motorcycle类。要求将vehicle作为虚基类,避免二义性问题。请在程序中
admin
2020-12-21
63
问题
请打开考生文件夹下的解决方案文件proj2,其中定义了vehicle类,并派生出motorcar类和bicycle类。然后以motorcar和bicycle作为基类,再派生出motorcycle类。要求将vehicle作为虚基类,避免二义性问题。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
80
150
100
1
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include
class vehicle
{
private:
int MaxSpeed;
int Weight;
public:
//**********found**********
vehicle(int maxspeed,int weight): ________
~vehicle(){};
int getMaxSpeed(){return MaxSpeed;}
int getWeight(){return Weight;}
};
//**********found**********
class bicycle: ________public vehicle
{
private:
int Height;
public:
bicycle(int maxspeed,int weight,int height):vehicle(maxspeed,weight),Height(height);
int getHeight(){return Height;};
};
//**********found**********
class motorcar: ________public vehicle
{
private:
int SeatNum;
public:
motorcar(int maxspeed,int weight,int seatntma):vehicle(maxspeed,weight),SeatNum(seatnum){}
iut getSeatNum(){return SeatNum;};
};
//**********found**********
class motorcycle: ________
{
public:
motorcycle(iut maxspeed,int weight,int height):vehicle(maxspeed,weight),bicycle(maxspeed,weight,
height),motorcar(maxspeed,weight,1){}
};
void main()
{
motorcycle a(80,150,100);
cout<<a.getMaxSpeed()<<endl;
cout<<a.getWeight()<<endl;
cout<<a.getHeight()<<endl;
cout<<a.getSeatNum()<<endl;
}
选项
答案
(1)MaxSpeed(maxspeed),Weighl(weiht){}; (2)v jnual (3)virtual (4)public bjcycle,public motorcar
解析
本题考查vehicle类及其派生类motorcar和bicy—cle,再由这两类派生出motorcycle类,其中涉及构造函数、成员函数和析构函数。构造函数采用成员初始化列表来完成对私有成员的初始化,派生类的书写要注意关键字。 (1)主要考查号生对构造函数的掌握,构造函数使用初始化列表来对私有成员Maxspeed和weight初始化。(2)主要考查号生对派生类的掌握,题目要求将vehicle作为虚基类,避免二义性问题。因此在这里添加vehicle使vehicle成为虚基类。(3)主要考查考生对派生类的掌握,派生出motorcycle类,在主函数可以看到motorcycle类的实例a调用getHeight函数和getseatNum函数,由此可知这两个基类都垃公有继承,因此得出语句:public bicycle,public motorear。虚基类继承时要添加关键字vinual,以避免二义性。
转载请注明原文地址:https://kaotiyun.com/show/uOyp777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
有如下程序: #include<iostream> using namespace std; int main() { int *p; *p=9; cout<<"The value at p:"<
已知函数f的原型是:voidf(int*a,long&B);,变量v1、v2;的定义是intv1;longv2;正确的调用语句是
已知数组arr的定义如下:intarr[5]={1,2,3,4,5}、下列语句中输出结果不是2的是
已知类MyClass声明如下:classMyClass{intn;public:MyClass(intk):n(k){}intgetValue()const{returnn;}};在下列数组定义中正确的是
有如下4个语句: ①count<<’A’<<setfill(’x’)<<left<<setw(7)<<’B’<<endl; ②count<<setfill(’x’)<<left<<setw(7)<<’A’<<’B’<<endl; ③cou
定义派生类时,若不使用关键字显式地规定采用何种继承方式,则默认方式为( )。
下列关于对象初始化的叙述中,正确的是
下列关于函数参数的叙述中,正确的是
下列关于模板形参的描述中,错误的是
如下函数模板:tempiateTsquare(Tx){returnx*x;}其中T是()。
随机试题
小说《苦恼》中展示了“人与人”之间的关系,也展示了“人与马”之间的关系。这两方面又构成了()
TheU.S.TravelAssociationconfirmedinasurveywhatmanyfrustratedfliersalreadyknow:TheNo.1airportheadacheispassen
腹膜炎手术后取半卧位的作用与哪项是无关的()。
下列说法错误的是
下列哪一项不能参加助理医师资格考试
患者,男,57岁。反复发作气急痰鸣30余年,平素易感冒,倦怠无力。现症见:气短声低,自汗,怕风,食少便溏,痰多质稀色白,舌质淡,苔白,脉细弱。其中医治法是
下列有关税款征收措施的说法,表述正确的有()。
2014年12月26日()正式通车,贵州跨入高铁时代。
2008年金融危机之后,西方发达国家中央银行纷纷通过压低市场利率的方法来促进本国经济复苏与实体经济发展,这导致了超低利率环境的出现,例如目前美国的联邦基金利率就稳定在0%至0.25%之间。超低利率环境改变了常规货币政策的传导渠道,导致了常规货币政策的传导效
Economics,asweknowit,isthesocialscienceconcernedwiththeproduction,distribution,exchange,andconsumptionofgoods
最新回复
(
0
)