c++多重派生,多重继承,虚拟继承例题

#include <iostream>
#include<string.h>
#include<math.h>
using namespace::std;
class CVehicle
{
public:
    CVehicle(int max_speed1, int speed1, int weight1) { max_speed = max_speed1; speed = speed1; weight = weight1; }
    int max_speed;
    int speed;
    int weight;
    CVehicle();
    ~CVehicle();
    void CVehicledisplay();
    void display1() {
        cout << "Vehicle" << ":" << endl;
        cout << "max_speed" << ":" << max_speed << endl;
        cout << "speed" << ":" << speed << endl;
        cout << "weight" << ":" << weight << endl;
        cout << endl;
    }
};
CVehicle::CVehicle(){}
CVehicle::~CVehicle(){}
class CBicycle :virtual public CVehicle
{
public:
    CBicycle();
    CBicycle(int max_speed1, int speed1, int weight1, int height1) :CVehicle(max_speed1, speed1, weight1) { height = height1; }
    int height;
    void display2() {
        cout << "Bicycle" << ":" << endl;
        cout << "max_speed" << ":" << max_speed << endl;
        cout << "speed" << ":" << speed << endl;
        cout << "weight" << ":" << weight << endl;
        cout << "height" << ":" << height << endl;
        cout << endl;
    }
};
CBicycle::CBicycle(){}
class CMotocar :virtual public CVehicle
{
public:
    CMotocar();
    CMotocar(int max_speed1, int speed1, int weight1,  int seat_num1) :CVehicle(max_speed1, speed1, weight1) { seat_num = seat_num1; }
    int seat_num;
    void display3() {
        cout << "Motocar" << ":" << endl;
        cout << "max_speed" << ":" << max_speed << endl;
        cout << "speed" << ":" << speed << endl;
        cout << "weight" << ":" << weight << endl;
        cout << "seat_num" << ":" << seat_num << endl;
        cout << endl;
    }
};
CMotocar::CMotocar(){}
class CMotocycle : public CBicycle,  public CMotocar
{
public:
    CMotocycle();
    CMotocycle(int max_speed1, int speed1, int weight1, int height1, int seat_num1) :CVehicle(max_speed1, speed1, weight1) ,CBicycle(max_speed1, speed1, weight1,height1) ,CMotocar(max_speed1, speed1, weight1,seat_num1){ seat_num = seat_num1; }  //注意此处的CBicycle和CMotocar的调用数量,应该与先前一致。
    void CMotocycledisplay();
    void display4() {
        cout << "Motocycle" << ":" << endl;
        cout << "max_speed" << ":" << max_speed << endl;
        cout << "speed" << ":" << speed << endl;
        cout << "weight" << ":" << weight << endl;
        cout << "height" << ":" << height << endl;
        cout << "seat_num" << ":" << seat_num << endl;
        cout << endl;
    }
};
CMotocycle::CMotocycle() {}
int main()
{
    int max_speed1;
    int speed1;
    int weight1;
    int height1;
    int seat_num1;
    cin >> max_speed1 >> speed1 >> weight1;
    cin >> height1;
    cin >> seat_num1;
    CMotocycle c(max_speed1,speed1, weight1, height1, seat_num1);
    c.display1();
    c.display2();
    c.display3();
    c.display4();
}

 一开始写了一堆set函数,发现其实用构造函数就能解决全部问题,跟OJ上提交的不同,在此处优化了一下,更加简洁,问题就在于我仍然全都是public成员,习惯挺差的说实话。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值