C++ enum Keyword

The enum keyword in C++ is used to define an enumeration, a user-defined data type that consists of a set of named integral constants. Enumerations improve code readability by providing meaningful names to values, reducing the risk of errors from using plain integers.

Starting from C++11, scoped enumerations (using enum class) were introduced, offering better type safety and scoping compared to traditional enumerations.


Syntax

</>
Copy
enum EnumName {
    constant1,
    constant2,
    constant3,
    ...
};

enum class EnumName {
    constant1,
    constant2,
    constant3,
    ...
};
enum
The keyword used to define a traditional enumeration.
enum class
The keyword used to define a scoped enumeration with better type safety.
EnumName
The name of the enumeration type.
constant1, constant2, constant3, …
The named constants within the enumeration, each representing an integral value.

Examples

Example 1: Traditional Enumeration

This example demonstrates the use of a traditional enum to define a set of constants representing weekdays.

</>
Copy
#include <iostream>
using namespace std;

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

int main() {
    Weekday today = Wednesday;

    if (today == Wednesday) {
        cout << "It's Wednesday!" << endl;
    }

    return 0;
}

Output:

It's Wednesday!

Explanation:

  1. The Weekday enumeration defines constants for the days of the week.
  2. The variable today is assigned the value Wednesday.
  3. The program checks if today is equal to Wednesday and prints the corresponding message.

Example 2: Scoped Enumeration

This example demonstrates the use of enum class to define a scoped enumeration for traffic light states.

</>
Copy
#include <iostream>
using namespace std;

enum class TrafficLight {
    Red,
    Yellow,
    Green
};

int main() {
    TrafficLight light = TrafficLight::Green;

    if (light == TrafficLight::Green) {
        cout << "Go!" << endl;
    }

    return 0;
}

Output:

Go!

Explanation:

  1. The TrafficLight scoped enumeration defines constants for traffic light states.
  2. The variable light is assigned the value TrafficLight::Green.
  3. The program checks if light is equal to TrafficLight::Green and prints “Go!”.

Example 3: Assigning Custom Values

This example shows how to assign custom integer values to enumeration constants.

</>
Copy
#include <iostream>
using namespace std;

enum ErrorCode {
    Success = 0,
    NotFound = 404,
    ServerError = 500
};

int main() {
    ErrorCode code = NotFound;

    cout << "Error code: " << code << endl;

    return 0;
}

Output:

Error code: 404

Explanation:

  1. The ErrorCode enumeration assigns custom integer values to its constants.
  2. The variable code is assigned the value NotFound, which corresponds to 404.
  3. The program prints the integer value of code.

Key Points about enum Keyword

  1. The enum keyword defines a traditional enumeration, while enum class defines a scoped enumeration.
  2. Enumerations assign meaningful names to integral values, improving code readability.
  3. Scoped enumerations (enum class) provide better type safety and require fully qualified names (e.g., TrafficLight::Green).
  4. Enumeration constants can have custom integer values.
  5. Enumerations are particularly useful for representing states, modes, or fixed sets of options.