C++ Enumeration

In C++, enumeration is a user-defined data type consisting of a set of named integer constants. Enumeration is used to make the code more readable, organized, and maintainable.

The syntax to define an enumeration is:

enum enum_name {constant1, constant2, constant3, ..., constantN};
Source:ww‮itfigi.w‬dea.com

Here, enum_name is the name of the enumeration, and constant1, constant2, constant3, ..., constantN are the named constants of the enumeration. Each named constant has an integer value associated with it, which is automatically assigned by the compiler.

For example, the following code defines an enumeration named Color with three named constants RED, GREEN, and BLUE:

enum Color {RED, GREEN, BLUE};

In this example, RED has a value of 0, GREEN has a value of 1, and BLUE has a value of 2. If we want to assign a specific value to a named constant, we can do so explicitly as follows:

enum Status {SUCCESS = 0, FAILURE = 1};

In this example, SUCCESS has a value of 0, and FAILURE has a value of 1.

Once an enumeration is defined, we can declare variables of that type and use the named constants as their values:

Color color = RED;

In this example, color is a variable of type Color with a value of 0, which corresponds to the named constant RED.