C++/S3/Enumerations
|
Bir programlama dili kitabı |
Temel Kavramlar |
|
Bu şablonu düzenle |
== In addition to the predefined types such as int and char, C++ allows you to define your own special data types. This can be done in several ways, the most powerful of which use classes as will be described later. We consider here a much simpler kind of user-defined type.
An enumeration type is an integral type that is defined by the user with the syntax
enum typename { enumerator-list };Here enum is a C++ keyword, typename stands for an identifier that names the type being defined, and enumerator-list stands for a list of names for integer constants. For example, the following defines the enumeration type Semester, specifying the three possible values that a variable of that type can have
enum Semester {FALL, SPRING, SUMMER}; We can then declare variables of this type:
Semester s1, s2 ; and we can use those variables and those type values as we would with predefined types:
s1 = SPRING; s2 = FALL; if (s1 == s2) cout << "Same semester." << endl;The actual values defined in the enumerator-list are called enumerators. In fact, they are ordinary integer constants. For example, the enumerators FALL, SPRING, and SUMMER that are defined for the Semester type above could have been defined like this:
const int FALL=0; const int WINTER=1; const int SUMMER=2;The values 0, 1, ... are assigned automatically when the type is defined. These default values can be overridden in the enumerator-list:
enum Coin {PENNY=1, NICKEL=5, DIME=10, QUARTER=25};If integer values are assigned to only some of the enumerators, then the ones that follow are given consecutive values. For example,
enum Month {JAN=1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV DEC } ;will assign the numbers 1 through 12 to the twelve months.
Since enumerators are simply integer constants, it is legal to have several different enumerators with the same value:
enum Answer {NO = 0, FALSE=0, YES = 1, TRUE=l, OK = 1}; This would allow the code
int answer; cin >> answer; . . . if (answer == YES) cout << "You said it was o.k." << endl; to work as expected. If the value of the variable answer is 1, then the condition will be true and the output will occur. Note that since the integer value 1 always means "true" in a condition, this selection statement could also be written
if (answer) cout << "You said it was o.k." << endl;Notice the conspicuous use of capitalization here. Most programmers usually follow these conventions for capitalizing their identifiers:
Use only upper-case letters in names of constants. Capitalize the first letter of each name in user-defined types. Use all lower-case letters everywhere else. These rules make it easier to distinguish the names of constants, types, and variables, especially in large programs. Rule 2 also helps distinguish standard C++ types like float and string from user-defined types like Coin and Month.
Enumeration types are usually defined to make code more self-documenting; i.e., easier for humans to understand. Here are a few more typical examples:
enum Sex {FEMALE, MALE}; enum Day {SUN, MON, TUE, WED, THU, FRI, SAT}; enum Radix {BIN=2, OCT=3, DEC=10, HEX=16}; enum Color {RED, ORANGE, YELLOW, GREEN, BLUE, VIOLET}; enum Rank {TWO=2 THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE,
TEN, JACK, QUEEN,KING, ACE};
enum Suit {CLUBS, DIAMONDS, HEARTS, SPADES}; enum Roman {1=1, V=5, X=10, L=50, C=100, D=500, M=1000};Definitions like these can help make your code more readable. But enumerations should not be overused. Each enumerator in an enumerator list defines a new identifier. For example, the definition of Roman above defines the seven identifiers I, V, X, L, C, D, and M as specific integer constants, so these letters could not be used for any other purpose within the scope of their definition.
Note that enumerators must be valid identifiers. So for example, this definition would not be valid
enum Grade {F, D, C-, C, C+, B-, B, B+, A-, A}; // ERRONEOUSbecause the characters + ' and ' - ' cannot be used in identifiers. Also, the definitions for Month and Radix shown above could not both be in the same scope because they both define the symbol OCT.
Enumerations can also be anonymous in C++:
enum {I=1, V=5, X=10, L=50, C=100, D=500, M=1000}; This is just a convenient way to define integer constants==