C++学习笔记(4)—枚举enum

版权声明:本博客主要记录学习笔记和遇到的一些问题解决方案,转载请注明出处! https://blog.csdn.net/u010982507/article/details/81229996

使用

1、语法格式:enum 类型名称 {枚举成员列表 }
2、枚举成员是常量,默认从0开始,也可以赋值,赋值后的下一个成员+1;

实例

#include <iostream>
#include <string>
using namespace std;

enum direction {
    UP,
    DOWN,
    LEFT = 5,
    RIGHT
};

void change(direction d) {
    switch (d) {
    case UP:
        cout << "UP" << endl;
    case DOWN:
        cout << "DOWN" << endl;
    case LEFT:
        cout << "LEFT" << endl;
    case RIGHT:
        cout << "RIGHT" << endl;
    default:
        break;
    }
}
int main() {

    // 默认从0开始
    cout << UP << endl;
    // 下一个会自动加1
    cout << DOWN << endl;
    // 给LEFT赋值为5,则RIGHT会加1成为6
    cout << LEFT << endl;
    cout << RIGHT << endl;
    change(RIGHT);
    system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/u010982507/article/details/81229996
今日推荐