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

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

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

#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;
}

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632