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 keyword enum
is used, followed by the name of the enumeration and the members in braces.
enum DaysOfTheWeek
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
};
In this example,
DaysOfTheWeek
is an enumeration with seven members:Monday
,Tuesday
,Wednesday
,Thursday
,Friday
,Saturday
, andSunday
.- By default,
Monday
has the value 0,Tuesday
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 DaysOfWeek
{
Monday = 1,
Tuesday = 2,
Wednesday = 3,
Thursday = 4,
Friday = 5,
Saturday = 6,
Sunday = 7
};
If the numbers are consecutive, you can only number the first of them (the rest will be assigned automatically in sequence).
enum DaysOfWeek
{
Monday = 1,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
};
Using Enumerations
Assigning Values
Once an enumeration is defined, you can assign values to variables of the enumeration type:
DaysOfTheWeek today = Monday;
Comparing Values
You can also compare these values using equality operators:
if (today == Monday)
{
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 is int
by default).
Enum to Integer
You can convert an enumeration value to its integer representation as follows:
int numericValue = static_cast<int>(Wednesday);
std::cout << numericValue << std::endl; // Output: 2
Integer to Enum
You can also convert an integer to an enumeration, as long as the value is valid for the enumeration:
DaysOfTheWeek day = static_cast<DaysOfTheWeek>(4);
std::cout << day << std::endl; // Output: 4 (depending on the implementation, it may require an additional cast to print the name)
Practical Examples
Usage in a Task Management Application
Suppose we are developing an application to manage tasks and we want to use enumerations to represent the status of a task:
enum TaskStatus
{
Pending,
InProgress,
Completed,
Canceled
};
class Task
{
public:
std::string Name;
TaskStatus Status;
void showStatus()
{
std::cout << "The task '" << Name << "' is in status: " << Status << std::endl;
}
};
int main()
{
Task task;
task.Name = "Study for the exam";
task.Status = InProgress;
task.showStatus(); // Output: The task 'Study for the exam' is in status: 1
return 0;
}
Usage in a Game
In game development, enumerations can be useful for representing different types of enemies or game states:
enum EnemyType
{
Goblin,
Orc,
Dragon
};
class Enemy
{
public:
EnemyType Type;
void showType()
{
std::cout << "This enemy is a: " << Type << std::endl;
}
};
int main()
{
Enemy enemy;
enemy.Type = Dragon;
enemy.showType(); // Output: This enemy is a: 2
return 0;
}