Enumeration Type
Learn about the user-defined data type in C++.
We'll cover the following
In this lesson, we’ll look at a data type that is defined by the user, known as the enum or enumerated type.
What is the enum data type?
Enumeration or enum type lets us assign names to integral constants. The enum
keyword is used to define an enumeration. It’s just a way to name integer values. This makes our code easier to read and maintain.
We are most likely to use enums when we want to ensure that the value(s) used in the program are from within the limited set of possible values.
Syntax
enum type-name {value1, value2, ... valueN};
The type-name
is the name of the enumeration. The compiler would, by default, assign the first value 0 to value1
, 1 to value2
, 2 to value3
, and so on.
However, we can set a specific value to an enum element as well.
enum type-name {value1 = 4, value2, value3, ... valueN};
If we don’t assign any specific value to any name, it will automatically be assigned the value of their predecessor, + 1. So, value2
would be equal to 5, value3
would be equal to 6, and so on.
For example, we’ve created an enumeration type called coffee
, where latte
, espresso
, cappuccino
, americano
and mocha
are the values of coffee
type. No other value can be part of the coffee
. We’ll define it as follows:
enum coffee {latte, espresso, cappuccino, americano, mocha};
To use this enumerated type, we’ll create an enum
type variable and assign it any of the enum
elements (enumerators). To use the enumerated type coffee
, let’s create the variable favorite
:
// enum type-name variableName;
enum coffee favorite;
Here, favorite
is the object of the enumerated type coffee
. This means favorite
can only have values equal to the enum
elements.
A simple program that demonstrates how enums work is shown below. Click “Run” to see the output of the program.
#include <iostream>using namespace std;enum trafficSignal {red, yellow, green};int main() {trafficSignal state;cout << "The 1st traffic signal's state is: " << state << endl;return 0;}
- In line 4, we define an enumerated type called
trafficSignal
. - In line 7, we create a variable called
state
of typetrafficSignal
. Now, we can initialize thestate
variable with any of the three values:red
,yellow
andgreen
.We cannot initialize the
state
variable with anything other than what is in theenum-list
. Initializing the variable or enumerator with any other value that is not in theenum-list
(sayblue
) or even an integer value (say0
,1
, or2
), will give an error. This is an example of how we ensure that the value used is from within a specific set of values. - In line 8, we initialize the
state
variable with the valuered
which, when displayed, outputs its associated integer0
.
Each enumerator or element in the
enum-list
should be unique. However, the integral constants associated with the enumerators or elements can be duplicated. If we had another element (sayorange
) in theenum-list
and we set it to0
, and then displayed it, it would not give any error and would be correct.
Here is another program where we iterate through the enum-list
and print each of the elements’ associated integer constants.
#include<iostream>using namespace std;enum rainbow {red, orange, yellow, green, blue, indigo, violet};int main(){for(int i = red; i <= violet; i++)cout << i << " ";return 0;}
Look at another program where we use enums with the switch
statement.
#include <iostream>using namespace std;enum menu {latte, espresso, cappuccino, americano, mocha};// 0, 1, 2, 3, 4int main(){menu coffee = mocha; // coffee == 4switch(coffee){case latte:{cout << "You have selected latte. " << endl;break;}case espresso:{cout << "You have selected espresso. " << endl;break;}case cappuccino:{cout << "You have selected cappuccino. " << endl;break;}case americano:{cout << "You have selected americano. " << endl;break;}case mocha:{cout << "You have selected mocha. " << endl;break;}}return 0;}
Why use enums?
We can use enums in the following instances:
- When we have integers representing specific states or values, and we want to group and name them. The assigned names, in turn, make our code more readable and easier to maintain.
- when we want to ensure that the value is taken from within a specific set of values
If we don’t associate an integer with any name within the enum, the compilers assign values starting from 0.