Geek Slack

Learn C++
    About Lesson


    C++ Enumeration


    C++ Enumeration

    In C++, an enumeration (enum) is a user-defined data type that consists of integral constants. Enumerations provide a way to define a set of named constants, which can make your code more readable and maintainable.

    1. Declaring and Using Enumerations

    Example of declaring and using an enumeration:

    #include <iostream>
    
    // Define an enumeration
    enum Weekday {
        Monday,
        Tuesday,
        Wednesday,
        Thursday,
        Friday,
        Saturday,
        Sunday
    };
    
    int main() {
        // Declare a variable of type Weekday
        Weekday today = Wednesday;
    
        // Output the value of today
        std::cout << "Today is ";
    
        // Switch statement to check the value of today
        switch (today) {
            case Monday:
                std::cout << "Monday";
                break;
            case Tuesday:
                std::cout << "Tuesday";
                break;
            case Wednesday:
                std::cout << "Wednesday";
                break;
            case Thursday:
                std::cout << "Thursday";
                break;
            case Friday:
                std::cout << "Friday";
                break;
            case Saturday:
                std::cout << "Saturday";
                break;
            case Sunday:
                std::cout << "Sunday";
                break;
            default:
                std::cout << "Unknown day";
                break;
        }
    
        std::cout << std::endl;
    
        return 0;
    }

    2. Assigning Values to Enumerators

    You can explicitly assign values to enumerators:

    #include <iostream>
    
    // Define an enumeration with explicit values
    enum Color {
        Red = 1,
        Green = 2,
        Blue = 4
    };
    
    int main() {
        // Declare a variable of type Color
        Color selectedColor = Blue;
    
        // Output the value of selectedColor
        std::cout << "Selected color: ";
    
        // Switch statement to check the value of selectedColor
        switch (selectedColor) {
            case Red:
                std::cout << "Red";
                break;
            case Green:
                std::cout << "Green";
                break;
            case Blue:
                std::cout << "Blue";
                break;
            default:
                std::cout << "Unknown color";
                break;
        }
    
        std::cout << std::endl;
    
        return 0;
    }

    Conclusion

    Enumerations in C++ provide a way to define a set of named constants, improving code readability and maintainability by assigning meaningful names to integral values. This chapter covered basic enumeration declaration, assigning values to enumerators, and using enumerations in switch statements. Understanding enumerations allows you to create more expressive and organized code in your C++ programs.