定义文件有三部分内容:
(1) 定义文件开头处的版权和版本声明。
(2) 对一些头文件的引用。
(3) 程序的实现体(包括数据和代码)。
1 #include <iostream> 2 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 4 using namespace std; 5 int main(int argc, char** argv) { 6 //定义枚举类型,并指定其枚举元素的值 7 enum color { 8 RED=3, 9 YELLOW=6, 10 BLUE=9 11 }; 12 13 //声明枚举变量a和b,并为枚举变量a赋初值 14 enum color a=RED; 15 color b; //合法,与C语言不同 16 17 // 输出枚举常量 18 cout<<"RED="<<RED<<endl; 19 cout<<"YELLOW="<<YELLOW<<endl; 20 cout<<"BLUE="<<BLUE<<endl; 21 22 //枚举变量的赋值和输出 23 b=a; 24 a=BLUE; 25 cout<<"a="<<a<<endl; 26 cout<<"b="<<b<<endl; 27 //a=100; 错误! 28 //a=6 也错误! 29 30 //枚举变量的关系运算 31 b=BLUE; // 枚举变量的赋值运算 32 cout<<"a<b="<<(a<b)<<endl; 33 return 0; 34 }