In situations where a variable can only take on a specific set of values, such as colors or genders, enumerations allow you to create new types and to declare variables whose values are restricted to the enumerated set.

Enumerations are declared by the keyword enum, followed by the type name, an opening brace ({), the enumerated values separated by commas, a closing brace (}), and a semicolon (;). Variables are then declared as an enumerated type, and they can only receive one of the enumerated values:

enum WorkDays { Monday, Tuesday, Wednesday, Thursday, Friday }; const int Saturday = 7; WorkDays WorkingDay; WorkingDay = Thursday; // Thursday is a member of the WorkDays // enumerated type and therefore can be // assigned to the WorkingDay variable. WorkingDay = Saturday; // Although Saturday is an integer // constant, it is not part of the // enumerated type WorkingDay and // therefore can’t be assigned to the // WorkingDay variable.

Microsoft Visual C++  .NET(c) Step by Step
Microsoft Visual C++ .NET(c) Step by Step
ISBN: 735615675
Year: 2003
Pages: 208

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net