Enumerated data types in c++
Enumerated data types
Simple integer constants are required to represent domain specific data. For example, Colour of an automobile can be specified by a set of statements using cost qualifiers:
Const int cRED =0;
Const int cBLUE = 1;
………
Int auto_colour;
Auto_colour=cBLUE;
However, there are no checks. Since variable auto_colour is an integer, the following assignments are valid:
auto_colour=-1;
……..
auto_colour=rand();
Of course, these statements lose the semantics; variable auto_colour doesn’t any longer represent a colour, it is just an integer. It is thus important to be able to tell the compiler: “Variable auto_colour is supported to represented a colour, that is one of the defined set of choices RED, BLUE,…” and then have the compiler check, pretty thoroughly, that auto_colour is only used in this way throughout the program. This leads to “enumerated data types”.
Enumerated data types are an alternative way of declaring a set of integer constant and defining some integer variables. They also introduce new distinct types and allow the compiler to do type checking. It is this additional type checking that makes enums worthwhile.
Const int cRED =0;
Const int cBLUE = 1;
………
Int auto_colour;
Auto_colour=cBLUE;
However, there are no checks. Since variable auto_colour is an integer, the following assignments are valid:
auto_colour=-1;
……..
auto_colour=rand();
Of course, these statements lose the semantics; variable auto_colour doesn’t any longer represent a colour, it is just an integer. It is thus important to be able to tell the compiler: “Variable auto_colour is supported to represented a colour, that is one of the defined set of choices RED, BLUE,…” and then have the compiler check, pretty thoroughly, that auto_colour is only used in this way throughout the program. This leads to “enumerated data types”.
Enumerated data types are an alternative way of declaring a set of integer constant and defining some integer variables. They also introduce new distinct types and allow the compiler to do type checking. It is this additional type checking that makes enums worthwhile.
The syntax for declaration of the enumerated data types is shown below:
Enum enum_type_name {enumeration list } variable_list;
The following is a simple example of an enum declaration:
Enum Colour { Ered, eBLUE, eYELLOW, eGREEN, eSILVERGREY, eBURGUNDY}
The entries in the enumeration list are just names (of constant values). The same rules apply as for any other C++ names. However, by convention, the entries in the enum list should have names that start with ‘e’ and continue with a sequence of capital letters. With Colour now defined, we can have variables of type Colour:
Colour auto_colour;
……
Auto_colour = eBURGUNDY;
The compiler will reject things like auto_colour=4. Depending on the compiler you are using, you may get just a “Warning” or you may get an error (really, you should get an error).
Enumerators are integers of some form. The compiler may chose to represent them using shorts, or as unsigned chars. The compiler chooses distinct values for each member of an enumeration. Normally, the first member has value 0, the second is 1, and so forth. In the above example, eRED would be a kind of integer constant 0, eSILVERGREY would be 4.
auto_colour = 4;// Wrong, rejected or at least warned by compiler
auto_colour = eSILVERGREY; // Fine, set auto_colour to
// (probably) the value 4
Comments
Post a Comment