zoukankan      html  css  js  c++  java
  • C++继承和派生练习(一)--关于vehicle基类

    1. Target:定义一个车(vehicle)基类
      具有MaxSpeed、Weight等成员变量,Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类。
      自行车(bicycle)类有高度(Height)等属性,汽车(motorcar)类有座位数(SeatNum)等属性。
      从bicycle和motorcar派生出摩托车(motorcycle)类,在继承过程中,注意把vehicle设置为虚基类。
    2. 代码如下:
    #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.测试截图

  • 相关阅读:
    CF785E Anton and Permutation
    P4054 [JSOI2009]计数问题
    P4396 [AHOI2013]作业
    AD PCB中各层的含义
    AD21 使用手册 快捷键(二)
    AD 汉化和界面恢复
    AD21 使用手册 快捷键(一)
    TINA-TI 安装
    B站下载 视频
    restful-work基本组件
  • 原文地址:https://www.cnblogs.com/FlyerBird/p/8995960.html
Copyright © 2011-2022 走看看