The keyword, enum, allows
you to create data types of your own. You must also specify the legal values
for the new data type when you create it. The following statement could be
used to create the data type, Sundaes,
You should capitalize an enumerated data type that you create so that it
is easy to differentiate it from built-in C++ data types such as int, double,
etc.
After an enumerated data type is created, you can then declare variables
of that new data type. For example, the following statement declares
two variables, mySundae and yourSundae, as variables of the data type, sundaes,
Sundaes mySundae, yourSundae;
The C++ compiler then identifies the values listed within the braces of
the original enum statement with the values 0, 1, 2, etc. In the example above,
chocolate would have the actual numeric value of 0, banana would have the
value of 1, etc. The statement,
mySundae = chocolate;
would assign to the variable, mySundae, the value, chocolate. However, the
C++ compiler actually assigns mySundae the integer value of 0.
If you wish to assign your own values to the predefined values, you could
use a statement like,
for example, where the numeric quantities may refer to the number of calories
in each type of ice cream sundae. However, you could not use the same value
for two or more different types of ice cream sundae.
The keyword typedef can be used to give a new name to an existing data type such as int. If you
wish to use the name wholenumber to declare a variable of type int, then you
can give the data type an alias of wholeNumber with typedef.