沉迷于c++ 2022-04-10 16:51 采纳率: 50%
浏览 43
已结题

c ++ 编写一个汽车类, 包含车辆的宽度、长度、高度、重量信息,可以设 定车辆的速度(包括速度的大小和方向, 为了编写方便, 假定只有前后 左右四个方向, 每次改变方向只能左转或者右转).

c ++ 编写一个汽车类, 包含车辆的宽度、长度、高度、重量信息,可以设 定车辆的速度(包括速度的大小和方向, 为了编写方便, 假定只有前后 左右四个方向, 每次改变方向只能左转或者右转).

  • 写回答

2条回答 默认 最新

  • CSDN专家-link 2022-04-10 16:56
    关注

    前后左右可以定义为枚举类型。重点就是改变方向函数么......

    #include <iostream>
    using namespace std;
    typedef enum _movedir
    {
        move_q = 0,  //前
        move_r = 1,    //右
        move_h = 2,  //后
        move_l = 3   //左
    }movedir;
    
    class car
    {
    private:
        movedir md;
        float speed;
        float width;
        float length;
        float height;
        float weight;
    public:
        car() {}
        car(float s,movedir m,float w,float l,float h,float t)
        {
            speed = s;
            md = m;
            width = w;
            length  =l;
            height = h;
            weight = t;
        }
        void turnright()
        {
            int d = (md+1)%4;
            md = (movedir)d;
        }
        void turnleft()
        {
            int d = (md+3)%4;
            md = (movedir)d;
        }
        void print()
        {
            cout<<"车宽:"<<width<<endl;
            cout<<"车长:"<<length<<endl;
            cout<<"车高:"<<height<<endl;
            cout<<"车重:"<<height<<endl;
            cout<<"速度:"<<speed<<endl;
            cout<<"方向:" ;
            switch(md)
            {
            case move_q:
                cout<<"向前";
                break;
            case move_r:
                cout<<"向右";
                break;
            case move_h:
                cout<<"向后";
                break;
            case move_l:
                cout<<"向左";
                break;
            }
            cout<<endl;
        }
    };
    
    int main()
    {
        car c(50,move_q,1.9,4.8,1.2,1000);
        c.turnright();
        c.turnright();
        c.turnleft();
        c.print();
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月30日
  • 已采纳回答 5月22日
  • 创建了问题 4月10日

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序