3.3 枚举常量

Posted: March 25, 2011 in C++

/*使用枚举常量, 即EnumberatedConstants, 的语法是: enum+类名+{值+,+值};
eg:enum Days {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};
如果没有特别声明, Monday值为0, Tuesday的值为1, 以此类推.

eg:enum KG {Banana=3, pear=1, Apple=7, Orange};
这里预先初始化了常量的值, 但是Orange没有被初始化, 它会跟随上面的Apple的值+1, 即Orange会变成8.
*/

#include <iostream>

using namespace std;

void main()
{
                 enum Days {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};
                 enum KG {Banana=3, pear=1, Apple=7, Orange};

                 cout<<“Monday= “<<Monday<<endl;
                 cout<<“Tuesday= “<<Tuesday<<endl;
                 cout<<“Wednesday= “<<Wednesday<<endl;
                 cout<<“Thursday= “<<Thursday<<endl;
                 cout<<“Friday= “<<Friday<<endl;
                 cout<<“Saturday= “<<Saturday<<endl;
                 cout<<“Sunday= “<<Sunday<<endl<<endl;
 
                 cout<<“Banana= “<<Banana<<endl;
                 cout<<“pear= “<<pear<<endl;
                 cout<<“Apple= “<<Apple<<endl;
                cout<<“Orange= “<<Orange<<endl;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s