enum实例
时间: 2023-08-10 21:20:31 浏览: 43
以下是一个简单的enum实例:
```cpp
#include <iostream>
using namespace std;
enum Color
{
Red,
Green,
Blue
};
int main()
{
Color c = Red;
switch (c)
{
case Red:
cout << "The color is Red." << endl;
break;
case Green:
cout << "The color is Green." << endl;
break;
case Blue:
cout << "The color is Blue." << endl;
break;
default:
cout << "Unknown color." << endl;
break;
}
return 0;
}
```
这段代码定义了一个颜色类型Color,包括Red、Green和Blue三个常量。在main函数中,将c初始化为Red,然后使用switch语句根据c的值输出相应的颜色。输出结果为"The color is Red."