🌟 Join our Telegram group for exclusive updates! Join Now Get Involved

Enums

C++ Enumerations

C++ Enumerations

In C++, an enumeration (enum) is a user-defined data type that consists of a set of named integer constants. Enumerations provide a way to create symbolic names for values, making the code more readable and less error-prone. Enumerations are often used to define a set of related named constants.

Basic Example:


#include <iostream>

// Define an enumeration named "Color"
enum Color {
    RED,
    GREEN,
    BLUE
};

int main() {
    // Declare a variable of type "Color"
    Color myColor = GREEN;

    // Use the enum values
    if (myColor == RED) {
        std::cout << "The color is red." << std::endl;
    } else if (myColor == GREEN) {
        std::cout << "The color is green." << std::endl;
    } else {
        std::cout << "The color is blue." << std::endl;
    }

    return 0;
}

In this example, the Color enum consists of three named constants: RED, GREEN, and BLUE. You can use these constants to represent different colors in your program.

Enumerators and Underlying Type:

By default, the underlying type of an enum is int, and the enumerators are assigned consecutive integer values starting from 0. You can explicitly specify the underlying type and set specific values for enumerators:


#include <iostream>

// Enum with explicit underlying type and values
enum class Status : char {
    OK = 0,
    ERROR = -1,
    PENDING = 1
};

int main() {
    // Declare a variable of type "Status"
    Status myStatus = Status::OK;

    // Use the enum values
    if (myStatus == Status::OK) {
        std::cout << "Operation successful." << std::endl;
    } else if (myStatus == Status::ERROR) {
        std::cout << "Error occurred." << std::endl;
    } else {
        std::cout << "Operation is pending." << std::endl;
    }

    return 0;
}

In this example, the Status enum has an underlying type of char, and the enumerators have explicit values.

Enum Class (Scoped Enumerations):

C++11 introduced enum classes, which provide scope to the enumerators and prevent their names from polluting the surrounding namespace. Enum classes are created using the enum class syntax.


#include <iostream>

// Enum class with explicit underlying type and values
enum class Month : int {
    JANUARY = 1,
    FEBRUARY,
    MARCH,
    // ...
};

int main() {
    // Declare a variable of type "Month"
    Month currentMonth = Month::FEBRUARY;

    // Use the enum values
    if (currentMonth == Month::JANUARY) {
        std::cout << "It's January." << std::endl;
    } else {
        std::cout << "It's not January." << std::endl;
    }

    return 0;
}

In this example, the Month enum class is used to represent months, and the enumerators are scoped under the Month namespace.

Enumerations are helpful for making code more readable and self-documenting, especially when dealing with sets of related constants. They provide a way to give meaningful names to numeric values, making the code more maintainable and less error-prone.

Cookies Consent

This website uses cookies to ensure you get the best experience on our website.

Cookies Policy

We employ the use of cookies. By accessing BYTEFOXD9, you agreed to use cookies in agreement with the BYTEFOXD9's Privacy Policy.

Most interactive websites use cookies to let us retrieve the user’s details for each visit. Cookies are used by our website to enable the functionality of certain areas to make it easier for people visiting our website. Some of our affiliate/advertising partners may also use cookies.