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.测试截图

  • 相关阅读:
    点击滚动到任意位置
    设置滚动条
    突破浏览器限制字体大小不能低于12px的限制
    font-family 字体及各大主流网站对比
    可视区尺寸改变的时候,重绘Echarts
    <input />
    switch (xx) { case xx: break ......}
    让页面随浏览器的窗口大小缩放而缩放
    对后端返回的 html 标签不识别的解决办法
    html 标签分类:块级元素、内联元素 ......
  • 原文地址:https://www.cnblogs.com/FlyerBird/p/8995960.html
Copyright © 2011-2022 走看看