前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >C++继承和派生练习(一)--关于vehicle基类

C++继承和派生练习(一)--关于vehicle基类

作者头像
Enterprise_
发布于 2018-05-18 07:21:37
发布于 2018-05-18 07:21:37
1.4K00
代码可运行
举报
文章被收录于专栏:小L的魔法馆小L的魔法馆
运行总次数:0
代码可运行
  1. Target:定义一个车(vehicle)基类 具有MaxSpeed、Weight等成员变量,Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类。 自行车(bicycle)类有高度(Height)等属性,汽车(motorcar)类有座位数(SeatNum)等属性。 从bicycle和motorcar派生出摩托车(motorcycle)类,在继承过程中,注意把vehicle设置为虚基类。
  2. 代码如下:
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#include<iostream>
#include<string>
using namespace std;
class vehicle {
private:
    double MaxSpeed;
    double Weight;
public:
    vehicle(double MS,double WE) {
        MaxSpeed = MS;
        Weight = WE;
    }
    ~vehicle() {

    }
    double GetMaxSpeed() {
        return MaxSpeed;
    }
    double GetWeight() {
        return Weight;
    }
    void Run() {
        cout << "The MaxSpeed of the vehicle is " << MaxSpeed << endl;
        cout << "The Weight of the vehicle is " << Weight << endl;
    }
    void  Stop() {
        cout << "The vehicle has stopped!!!" << endl;
    }
};
class bicycle :virtual public vehicle {
private:
    double Height;
public:
    bicycle(double MS,double WE,double HE):vehicle(MS, WE) {
        Height = HE;
    }
    ~bicycle() {

    }
    double GetHeight() {
        return Height;
    }
    void Run() {
        cout << "The height of the bicycle is "<< Height << endl;
    }
    void Stop() {
        cout << "The bicycle has stopped!" << endl;
    }
};
class motorcar :virtual vehicle {
private:
    int SeatNum;
public:
    motorcar(double MS, double WE,  int SN):vehicle(MS,WE) {
        SeatNum = SN;
    }
    ~motorcar() {

    }
    int GetSeatNum() {
        return SeatNum;
    }
    void Run() {
        cout << "The SeatNum of the motorcar is "<< SeatNum << endl;
    }
    void Stop() {
        cout << "The motorcar has stopped!" << endl;
    }
};
class motorcycle :public bicycle, public motorcar {
public:
    motorcycle(double m, double w, double h, double n) :vehicle(m, w), bicycle(m, w, h), motorcar(m, w, n) {

    }
    ~motorcycle() {

    }
    void Run() {
        cout << "This is a motorcycle:" << endl;
        cout << "MaxSpeed:" << GetMaxSpeed() << endl;
        cout << "Weight:" << GetWeight() << endl;
        cout << "Height:" << GetHeight() << endl;
        cout << "SeatNum:" << GetSeatNum() << endl;
    }
    void Stop() {
        cout << "The motorcycle has stopped!!!" << endl;
    }
};
int main() {
    vehicle  t1(12.0, 12.0);
    t1.Run();
    t1.Stop();
    bicycle t2(16.0, 15.2, 36);
    t2.Run();
    t2.Stop();
    motorcar t3(13, 65, 33);
    t3.Run();
    t3.Stop();
    motorcycle t4(12, 56, 8, 4);
    t4.Run();
    t4.Stop();
    return 0;
}

3.测试截图

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018年05月01日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档