Enumerations
Enumerations are data aggregates that allow naming a set of integers.
For example, by writing:
enum position
{
left,
center,
right
};
a set of three integers is defined, beginning from 0. left can be used for 0,
center for 1, right for 2.
In case it is necessary to make an element correspond to a specific integer,
an assignment can be made. Each subsequent member will correspond to the
integer assigned increased by one at each member.
Given an enumeration:
enum Color
{
black,
white,
blue,
red = 10,
green,
yellow
};
The values corresponding to each element are:
black 0
white 1
blue 2
red 10
green 11
yellow 12
Example
#include <iostream>
using namespace std;
enum Continent
{
Europe, // corresponds to 0
Africa,
Asia,
North_America,
South_America,
Oceania
};
enum Color
{
black,
white,
blue,
red = 10,
green,
yellow
};
int main()
{
Continent aContinent;
aContinent = Asia;
cout << "aContinent = Asia; : ";
cout << aContinent << endl;
Color aColor;
aColor = white;
cout << "aColor = white; : ";
cout << aColor << endl;
aColor = yellow;
cout << "aColor = yellow; : ";
cout << aColor << endl;
}
Output
aContinent = Asia; : 2
aColor = white; : 1
aColor = yellow; : 12