Introduction to C++ | Enumeration

C++ Enumeration (enum)

An enum (short for enumeration) in C++ is a user-defined data type consisting of a set of named integer constants. Enums are typically used to represent a collection of related values, making the code more readable and maintainable. Instead of using arbitrary numbers to represent options, you can define meaningful names for the values, which improves the clarity of the code.

What is an Enum?

An enum defines a set of named integral constants that can be used to represent discrete values. By default, the first name in the enumeration has the value 0, and each subsequent name has a value that is one greater than the previous name. However, you can explicitly assign values to the names.

Syntax of Enum

The syntax for defining an enumeration is as follows:

Syntax of Enum


        enum EnumName {
            NAME1,   // Default starts with 0
            NAME2,   // 1
            NAME3,   // 2
            // More names
        };
                    

Example: Simple Enum

Here’s a simple example of defining and using an enum:

Code Example: Simple Enum


        #include <iostream>
        using namespace std;
        
        // Define the enum
        enum Day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
        
        int main() {
            Day today = Wednesday;
        
            // Display the value of today
            cout << "The value of today is: " << today << endl;
        
            return 0;
        }
                    

Output:

The value of today is: 3

Enum with Explicit Values

You can explicitly assign values to the enumerators. This can be useful when you need to map specific values to your enum constants.

Example: Enum with Explicit Values

Here’s an example where each day of the week is explicitly assigned a value:

Code Example: Enum with Explicit Values


        #include <iostream>
        using namespace std;
        
        // Define the enum with explicit values
        enum Day { Sunday = 1, Monday, Tuesday, Wednesday, Thursday = 10, Friday, Saturday };
        
        int main() {
            Day today = Wednesday;
        
            // Display the value of today
            cout << "The value of today is: " << today << endl;
        
            return 0;
        }
                    

Output:

The value of today is: 4

Accessing Enum as Integer

You can access the underlying integer value of an enum by casting it to an integer type. This is useful when you need to perform operations based on the numeric value of the enum.

Example: Accessing Enum as Integer

Here’s an example of accessing the integer value of an enum:

Code Example: Accessing Enum as Integer


        #include <iostream>
        using namespace std;
        
        enum Day { Sunday = 1, Monday, Tuesday, Wednesday, Thursday = 10, Friday, Saturday };
        
        int main() {
            Day today = Friday;
        
            // Access the integer value of the enum
            cout << "The numeric value of today is: " << static_cast<int>(today) << endl;
        
            return 0;
        }
                    

Output:

The numeric value of today is: 6

Enum with Functions

Enums can also be used in functions. You can pass enum values to functions or return an enum value from a function. This makes the code more readable and less error-prone.

Example: Enum with Functions

Here’s an example where an enum is passed to a function:

Code Example: Enum with Functions


        #include <iostream>
        using namespace std;
        
        enum Day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
        
        void printDay(Day d) {
            switch (d) {
                case Sunday: cout << "Sunday" << endl; break;
                case Monday: cout << "Monday" << endl; break;
                case Tuesday: cout << "Tuesday" << endl; break;
                case Wednesday: cout << "Wednesday" << endl; break;
                case Thursday: cout << "Thursday" << endl; break;
                case Friday: cout << "Friday" << endl; break;
                case Saturday: cout << "Saturday" << endl; break;
                default: cout << "Invalid day" << endl; break;
            }
        }
        
        int main() {
            Day today = Friday;
            printDay(today);
        
            return 0;
        }
                    

Output:

Friday

Pro Tip:

💡 Pro Tip

Enums are a great way to represent discrete options with meaningful names, making your code easier to understand. Use enums for things like days of the week, months of the year, or status codes. When assigning values to enums, it's a good practice to make the values start from 0 or another logical base value, and to increment by 1 unless there’s a specific need for custom values.