程序填空题:车辆类虚基类
在下面一段类的定义中,自行车类的虚基类为车辆类,机动车类的虚基类也为车辆类,摩托车类的基类为自行车类和机动车类,类之间均为公有继承。
c++
#include <iostream>
using namespace std;
class vehicle //车辆类
{
private:
int MaxSpeed; //最大车速
int Weight; //车重
public:
vehicle(){MaxSpeed=0; Weight=0;}
virtual void Run() {cout<<"A vehicle is running!"<<endl;}
};
class bicycle : @@[virtual public vehicle](2) //自行车类
{
private:
int Height; //车高
public:
bicycle(){}
void Run() {cout<<"A bicycle is running!"<<endl;}
};
class motorcar : @@[virtual public vehicle](2) //机动车类
{
private:
int SeatNum; //乘人数
public:
motorcar(){}
void Run() {cout << "A motorcar is running!" << endl; }
};
class motorcycle: @@[public bicycle, public motorcar](2) //摩托车类
{
public:
motorcycle (){}
void Run() {cout<<"A motorcycle is running!"<<endl;}
};
int main() {
motorcycle a;
a.Run();
return 0;
}
答案:
第1空:virtual public vehicle
第2空:virtual public vehicle
第3空:public bicycle, public motorcar
c++
#include <iostream>
using namespace std;
class vehicle //车辆类
{
private:
int MaxSpeed; //最大车速
int Weight; //车重
public:
vehicle(){MaxSpeed=0; Weight=0;}
virtual void Run() {cout<<"A vehicle is running!"<<endl;}
};
class bicycle : @@[virtual public vehicle](2) //自行车类
{
private:
int Height; //车高
public:
bicycle(){}
void Run() {cout<<"A bicycle is running!"<<endl;}
};
class motorcar : @@[virtual public vehicle](2) //机动车类
{
private:
int SeatNum; //乘人数
public:
motorcar(){}
void Run() {cout << "A motorcar is running!" << endl; }
};
class motorcycle: @@[public bicycle, public motorcar](2) //摩托车类
{
public:
motorcycle (){}
void Run() {cout<<"A motorcycle is running!"<<endl;}
};
int main() {
motorcycle a;
a.Run();
return 0;
}
答案:
第1空:virtual public vehicle
第2空:virtual public vehicle
第3空:public bicycle, public motorcar