In C++, an enumeration (or enum) is a way to define a set of named integer values under a single 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 improve code readability and maintainability by replacing magic numbers with descriptive names.
If you want to learn more, check out the Introduction to Programming Course
Defining an Enumeration
To define an enumeration in C++, use the keyword enum, followed by the enumeration name and its members within braces.
enum DiasDeLaSemana
{
Lunes,
Martes,
Miércoles,
Jueves,
Viernes,
Sábado,
Domingo
};
In this example,
DiasDeLaSemanais an enumeration with seven members:Lunes,Martes,Miércoles,Jueves,Viernes,Sábado, andDomingo.- By default,
Luneshas the value 0,Marteshas the value 1 (and so on).
Enumerations with specific values
It is possible to assign specific values to enumeration members (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 sequential, you can number only the first one (the rest will be assigned automatically in sequential order).
enum DiasSemana
{
Lunes = 1,
Martes,
Miércoles,
Jueves,
Viernes,
Sábado,
Domingo
};
Using Enumerations
Value Assignment
Once an enumeration is defined, you can assign values to variables of the enumeration type:
DiasDeLaSemana hoy = Lunes;
Value Comparison
You can also compare these values using equality operators:
if (hoy == Lunes)
{
std::cout << "Today is Monday." << std::endl;
}
Enumeration Conversion
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 to create 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,
Coloris a scoped enumeration with the valuesRojo,Verde, andAzul.- Each value is associated with a
chartype.
Practical Examples
Usage in a Task Application
Suppose we are developing an application to manage tasks and 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;
}
Usage in a Game
In game development, enumerations can be useful for representing different enemy types 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;
}
