虚基类的一个小问题
一道练习题 不知道哪里错了 编译说no accessible path to public member declared in virtual base 'CVehicle'
程序如下
//定义一个车类CVehicle作为基类,具有max_speed、weight等数据成员,Run、Stop等成员函数,
//由此派生出自行车类CBicycle、汽车类CMotocar。自行车类CBicycle有高度height等属性,
//汽车类CMotocar有座位数seat_num等属性,从CBicycle和CMotocar派生出摩托车类CMotocycle,
//在派生过程中,注意把CVehicle设置为虚基类。
#include<iostream.h>
class CVehicle
{
protected:
int max_speed;
int weight;
public:
CVehicle(){}
void Run(){}
void Stop(){}
void set_max_speed(int maxspeed)
{max_speed=maxspeed;}
void set_weight(int wei)
{weight=wei;}
int get_max_speed() //返回
{return max_speed;}
int get_weight()
{return weight;}
};
class CBicycle:virtual CVehicle
{
protected:
int height;
public:
CBicycle(){}
void set_height(int hei)
{height=hei;}
int get_height()
{return height;}
};
class CMotocar:virtual CVehicle
{
protected:
int seat_num;
public:
CMotocar(){}
void set_seat_num(int seatnum)
{seat_num=seatnum;}
int get_seatnum()
{return seat_num;}
};
class CMotocycle:public CBicycle,public CMotocar
{};
void main()
{
CMotocycle a;
a.set_max_speed(100);
a.set_weight(100);
a.set_height(1);
a.set_seat_num(2);
cout<<"CMotocycle:"
<<"\nmaxspeed:"<<a.get_max_speed()
<<"\nweight:"<<a.get_weight()
<<"\nheight:"<<a.get_height()
<<"\nseatnum:"<<a.get_seat_num();
}