首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,其中定义了vehicle类,并派生出mot。rcar类和bicycle类。然后以mottorcar和bicycle作为基类,再派生出motorcycle类。要求将vehicle作为虚
请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,其中定义了vehicle类,并派生出mot。rcar类和bicycle类。然后以mottorcar和bicycle作为基类,再派生出motorcycle类。要求将vehicle作为虚
admin
2017-07-28
51
问题
请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,其中定义了vehicle类,并派生出mot。rcar类和bicycle类。然后以mottorcar和bicycle作为基类,再派生出motorcycle类。要求将vehicle作为虚基类,避免二义性问题。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
80
150
100
1
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include<iostream.h>
class vehicle
{
private:
int MaxSpeed;
int Weight;
public:
//**********found**********
vehicle(intmaxspeed,intweight):_______
~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 seatnum):vehicle(maxspeed,weight),SeatNum(seatnum){}
int getSeatNum(){return SeatNum;};
};
//**********found**********
classmotorcycle:______________
{
public:
motorcycle(int maxspeed,int weight,int height):vehicle(maxspeed,weight),bicycle(maxspeed,weight,height),motorcar(maxspeed,weight,1){}
};
void main()
{
motorcycle a(80,150,10);
cout<<a.getMaxSpeed()<<endl;
cout<<a.getWeight()<<endl;
cout<<a.getHeight()<<endl;
cout<<a.getSeatNum()<<endl;
}
选项
答案
(1)MaxSpeed(maxspeed),Weight(weight){}; (2)virtual (3)virtual (4)public bicycle,public motorcar
解析
本题考查vehicle类及其派生类motorcar和bicvcle,再由这两类派生出motorcycle类,其中涉及构造函数、成员函数和析构函数。构造函数采用成员初始化列表来完成对私有成员的初始化,派生类的书写要注意关键字。
(1)主要考查考生对构造函数的掌握,构造函数使用初始化列表来对私有成员MaxSpeed和weight初始化。
(2)主要考查考生对派生类的掌握,题目要求将Vehicle作为虚基类,避免二义性问题。因此在这里添加virtual使vehicle成为虚基类。
(3)主要考查考生对派生类的掌握,题目要求以motorcar和bicycle作为基类,再派生出motorcycle类。在主函数中可以看到motorcycle类的实例a调用getHeight函数和getSeatNum函数,由此可知这两个基类都是公有继承,因此得出语句:public bicycle,public motorCar。
虚基类继承时要添加关键字virtual,以避免二义性。
转载请注明原文地址:https://kaotiyun.com/show/BFAp777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
有如下类的定义。应在空格处填入的语句是()。classMyClass{______________intx,y;public:MyClass(inta=0,intb=0)
有以下程序:#include<iostream>usingnamespacestd;classBASE{private:charc;public:BASE(charn
有以下程序:#include<iostream>usingnamespaceStd;intmain(){inta=15,b=21,m=0;switch(a%3)
下面程序的输出结果是()。#include<iostream>usingnamespaceStd;intmain(){intx[6]={1,3,5,7,9,11},*k,**s;
在重载一个运算符函数时,其参数表中没有任何参数,这说明该运算符是()。
对于下面程序,对p进行赋值正确的是( )。 class A{ public:fun(int i){cout<<i<<endl;} }; main(){ void(A::*p)(int); }
下列有关内联函数的叙述中,正确的是()。
以下各选项组中,均是C++语言关键字的组是
应用数据库的主要目的是
判断字符型变量ch是否为大写英文字母,应使用表达式
随机试题
A、Hethoughtitwascool.B、Heneededthepractice.C、Hewantedtostayconnectedwiththem.D、Hehadanurgentmessagetosend.
BX3型弧焊机焊接电流粗调是通过改变()来实现的。
单相半波相控整流电路,负载电阻Rd=20Ω,直接接在交流220V电源上,当触发延迟角α=30°时,求输出电压平均值Ud、输出电流平均值Id和有效值I。
按照担保法的规定,下列合同中,可以附最高额抵押合同的是()
A.胞液B.线粒体C.核糖体D.高尔基复合体E.内质网糖有氧氧化过程中进行三羧酸循环的部位
根据《医疗事故处理条例》,患者一方向卫生行政部门提出医疗事故争议处理申请的期限为自知道或应当知道发生人身损害之日起
以下各选项中,属于流动资产的有()。
材料:齐白石先生幼年家境贫寒,没有上学的机会,长大后做了木工,四十岁以后才开始自学绘画。他虚心求教,勤学苦练,终于在画坛上独树一帜,成为著名的国画大师。问题:试述影响人的发展的主要因素。
以下程序的输出结果是【】。#include<stdio.h>main(){intn=12345,d;while(n!=0){d=n%10;printf("%d",D);n/=10;}
InthefirstyearorsoofWebbusiness,mostoftheactionhasrevolvedaroundeffortstotaptheconsumermarket.Morerecentl
最新回复
(
0
)