C++ Enumeration

Enumeration, or “enum” for short, is a user-defined data type in C++ that consists of a set of named values. Enums are used to represent a group of related values and can be useful in situations where a set of named constants is needed. Enums are similar to constants, but they are more powerful and flexible.

Declaring an Enumeration

An enumeration is defined using the “enum” keyword, followed by the enumeration name and a set of enumerators enclosed in curly braces. Each enumerator is a constant of the type int and has a unique name. For example:

enum Weekday {
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
};

In this example, “Weekday” is the name of the enumeration and “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, and “Sunday” are the enumerators. By default, the first enumerator has the value 0, the second has the value 1, and so on. However, it is also possible to explicitly specify the values of the enumerators:

enum Weekday {
    Monday = 1,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday = 7
};

In this example, “Monday” has the value 1, “Tuesday” has the value 2, “Wednesday” has the value 3, “Thursday” has the value 4, “Friday” has the value 5, “Saturday” has the value 6 and “Sunday” has the value 7.

Using Enumerations

Enumerations can be used in the same way as any other data type. For example, an enumeration variable can be declared and initialized:

Weekday today = Monday;

Enumerators can also be used in expressions and in comparison with other enumerators or integer values:

if (today == Monday) {
    std::cout << "Today is Monday" << std::endl;
}
int tomorrow = (today + 1) % 7; // Monday + 1 = Tuesday
if (tomorrow == Tuesday) {
std::cout << "Tomorrow is Tuesday" << std::endl;
}

Enumeration Classes

C++11 introduced a new type of enumeration called “enum class” (or “scoped enumeration”). An enumeration class is similar to an ordinary enumeration, but with a few key differences:

  • Enumeration classes have their own scope, which means that enumerators are not in the same scope as other variables or functions.
  • Enumeration classes are strongly typed, which means that an enumerator of one enumeration class cannot be used where an enumerator of another enumeration class is expected.
  • Enumeration classes can have non-integer underlying types, such as short or long long.

An enumeration class is defined using the “enum class” keyword, followed by the enumeration class name and a set of enumerators enclosed in curly braces. For example:

enum class Weekday {
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
};

To use an enumeration class, the enumeration class name must be specified along with the enumerator, like this:

Weekday today = Weekday::Monday;

It is also possible to specify the underlying type of an enumeration class by using the “: type” syntax. For example:

enum class Weekday : short {
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
};

In addition to these features, enumeration classes also prevent implicit conversions between enumerators and integers, which can help to prevent bugs in your code. This makes enumeration classes safer to use than ordinary enumerations in certain situations.

Conclusion

Enumerations provide a way to define a set of named values in C++. They are useful in situations where a set of named constants is needed. Enumerations are similar to constants, but they are more powerful and flexible. Enumeration classes, introduced in C++11, provide additional features such as scoping and strong typing. Enumeration classes can also have non-integer underlying types, which can help to prevent bugs in your code.

In conclusion, Enumerations in C++ are a powerful tool that provides a way to define named values and can be used to increase the readability, maintainability, and safety of your code.

Leave a Reply

Your email address will not be published. Required fields are marked *