虚基类的一个小问题

jacky_ji 2005-11-27 11:14:30
一道练习题 不知道哪里错了 编译说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();
}
...全文
107 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
jacky_ji 2005-11-27
  • 打赏
  • 举报
回复
3Q3Q
改好了
//定义一个车类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:public virtual CVehicle //默认为私有的继承
{
protected:
int height;
public:
CBicycle(){}
void set_height(int hei)
{height=hei;}
int get_height()
{return height;}
};
class CMotocar:public virtual CVehicle
{
protected:
int seat_num;
public:
CMotocar(){}
void set_seat_num(int seatnum)
{seat_num=seatnum;}
int get_seat_num()
{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();
}
snowbirdfly 2005-11-27
  • 打赏
  • 举报
回复
virtual CVehicle
默认为私有继承~~
所以a无法访问set_max_speed这些成员函数~~~
所以改为公有派生就可以访问了`~
snowbirdfly 2005-11-27
  • 打赏
  • 举报
回复
#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 public CVehicle
{
protected:
int height;
public:
CBicycle(){}
void set_height(int hei)
{height=hei;}
int get_height()
{return height;}
};
class CMotocar:virtual public CVehicle
{
protected:
int seat_num;
public:
CMotocar(){}
void set_seat_num(int seatnum)
{seat_num=seatnum;}
int get_seat_num()
{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();
}
这样应该可以~~
楼主还是好好看看C++~~~

65,206

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧