枚举

#include <iostream>

using namespace std;

int main()
{
//定义枚举类型,并指定其枚举元素的值
enum color {
RED = 3,
YELLOW = 6,
BLUE = 9
};
//声明枚举变量a和b,并为枚举变量a赋初值
enum color a = RED;
color b; //合法,与C语言不同

// 输出枚举常量 
cout << "RED = " << RED << endl;
cout << "YELLOW = " << YELLOW << endl;
cout << "BLUE = " << BLUE << endl;

//枚举变量的赋值和输出

b = a;
a = YELLOW;
cout << "a = " << a << endl;
cout << "b = " << b << endl;

//枚举变量的关系运算
b = BLUE;      // 枚举变量的赋值运算
cout << a << endl;
cout << b << endl;
cout << "a < b = " << (a < b) << endl;

}

上一篇:枚举、结构体、共用体【Java】


下一篇:Effective C++ 条款2:尽量以const,enum,inline替换#define