In C++ an enumeration (or enum
) is a way to define a set of named integer values under the same type.
Each member of an enumeration is a constant that represents a unique integer value (starting at 0 by default and increasing by one for each subsequent member)-
Enumerations make code easier to read and maintain by replacing magic numbers with descriptive names.
If you want to learn more about Enumerations
check the Introduction to Programming Course read more ⯈
Definition of an Enumeration
To define an enumeration in C++, the enum
keyword is used, followed by the name of the enumeration and the members in braces.
enum DiasDeLaSemana
{
Lunes,
Martes,
Miércoles,
Jueves,
Viernes,
Sábado,
Domingo
};
In this example,
DiasDeLaSemana
is an enumeration with seven members:Lunes
,Martes
,Miércoles
,Jueves
,Viernes
,Sábado
, andDomingo
.- By default,
Lunes
has the value 0,Martes
has the value 1 (and so on)
Enumerations with specific values
It is possible to assign specific values to the members of an enumeration (instead of using the default values).
enum DiasSemana
{
Lunes = 1,
Martes = 2,
Miércoles = 3,
Jueves = 4,
Viernes = 5,
Sábado = 6,
Domingo = 7
};
If the numbers are consecutive, you can only number the first one (the rest will be assigned automatically in sequential order).
enum DiasSemana
{
Lunes = 1,
Martes,
Miércoles,
Jueves,
Viernes,
Sábado,
Domingo
};
Use of enumerations
Assigning values
Once an enumeration is defined, you can assign values to variables of the enumeration type:
DiasDeLaSemana hoy = Lunes;
Comparing values
You can also compare these values using equality operators:
if (hoy == Lunes)
{
std::cout << "Today is Monday." << std::endl;
}
Conversion of enumerations
Enumerations in C++ are based on integer data types. Therefore, it is possible to convert between an enumeration and its underlying type (which by default is int
).
You can convert an enumeration value to its integer representation as follows:
int valorNumerico = static_cast<int>(Miércoles);
std::cout << valorNumerico << std::endl; // Output: 2
You can also convert an integer to an enumeration, as long as the value is valid for the enumeration:
DiasDeLaSemana dia = static_cast<DiasDeLaSemana>(4);
std::cout << dia << std::endl; // Output: 4 (depending on the implementation, it may require an additional cast to print the name)
Scoped enumerations
Scoped enumerations (enum class
or enum struct
) are a safer way to define enumerations in C++, as the values of a scoped enumeration do not implicitly convert to integers and do not enter the global scope.
The syntax for creating a scoped enumeration
enum class NombreEnum : tipo_base {
Valor1,
Valor2,
// More values
};
For example,
enum class Color : char {
Rojo,
Verde,
Azul
};
Color c = Color::Rojo;
In this example,
Color
is a scoped enumeration with the valuesRojo
,Verde
, andAzul
- Each value is associated with a
char
type.
Practical examples
Use in a task application
Suppose we are developing an application to manage tasks and we want to use enumerations to represent the status of a task:
enum EstadoTarea
{
Pendiente,
EnProgreso,
Completada,
Cancelada
};
class Tarea
{
public:
std::string Nombre;
EstadoTarea Estado;
void mostrarEstado()
{
std::cout << "The task '" << Nombre << "' is in state: " << Estado << std::endl;
}
};
int main()
{
Tarea tarea;
tarea.Nombre = "Study for the exam";
tarea.Estado = EnProgreso;
tarea.mostrarEstado(); // Output: The task 'Study for the exam' is in state: 1
return 0;
}
Use in a game
In game development, enumerations can be useful for representing different types of enemies or game states:
enum TipoDeEnemigo
{
Goblin,
Orco,
Dragon
};
class Enemigo
{
public:
TipoDeEnemigo Tipo;
void mostrarTipo()
{
std::cout << "This enemy is a: " << Tipo << std::endl;
}
};
int main()
{
Enemigo enemigo;
enemigo.Tipo = Dragon;
enemigo.mostrarTipo(); // Output: This enemy is a: 2
return 0;
}
:::::::