简答题 C++
题号 #2607983

请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,其中定义了vehiele类,并派生出motorcar类和bicycle类。然后以motorcar和bicycle作为基类,再派生出motorcycle类。要求将Vehicle作为虚基类,避免二义性问题。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
80
150
100
1
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include<iostream.h>
class vehicle
{
private:
int MaxSpeed;
int Weight;
public:
//*************found************
vehicle(int maxspeed,int weight):——
~vehicle(){};
int getMaxSpeed(){return MaxSpeed;}
int getWeight(){retum Weight;}
};
//****************found************
class bicycle:——public vehicle
{
private:
int Height;
public:
bicycle(int maxspeed,int weight,int height):vehicle(maxspeed,weight),Height(height){}
int getHeight(){retum 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***************
class motorcycle:——
{
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,100);
cout<<a.getMaxSpeed()<<endl;
cout<<a.getWeight()<<endl;
cout<<a.getHeight()<<endl;
cout<<a.getSeatNum()<<endl;
}

进入在线模考

参考答案

暂无

答案解析

(1)MaxSpeed(maxspeed),Weight(weight){f;
(2)virtual
(3)virtua1
(4)public bicycle,public motorcar

你可能感兴趣的试题

1

请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中声明的CDeepCopy是一个用于表示矩阵的类。请编写这个类的赋值运算符成员函数0perator=,以实现深层复制。
要求:
补充编制的内容写在“//************333************”与“//****************666*************”之间。不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//CDeepCopy.h
#include  <iostream>
#include<string>
using namespace std;
class CDeepCopy
{
public:
int n;//动态数组的元素个数
int  *p;//动态数组首地址
CDeepCopy(int);
~CDeepCopy();
CDeepCopy&operator=(const CDeepCopy&r);//赋值运算符函数
};
void writeToFile(char});
//main.cpp
#include”CDeepCopy.h”
CDeepCopy::一CDeepCopy(){delete[]P;}
CDeepCopy::CDeepCopy(int k){n=k;p=new int[n];}//构造函数实现
CDeepCopy&CDeepCopy::0perator=(const CDeepCopy&r)//赋值运算符函数实现
{
//***********333*********
//***********666***********
}
int main()
{
CDeepCopy a(2),d(3);
a.p[O]=1;d.p[O]=666;//对象a,d数组元素的赋值
{
CDeepCopy b(3);//调用赋值运算符函数
a.p[O]=88;b=a;
cout<<b.p[O];//显示内层局部对象的数组元素
}
cout<<d.p[O];//显示d数组元素a.p[O]的值
cout<<”d fade away;\n”;
cout<<a.p[O];//显示a数组元素a.p[O]的值
writeToFile(””);
return 0;
}

找试题 · 找答案 · 找资料 在线模考