Open In App

Enumeration in C++

Last Updated : 26 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, enumeration is a user-defined data type that consists of a set of named integer constants. It helps in assigning meaningful names to integer values to improve code readability and maintainability.

Create Enum

An enum needs to be defined before we can create its variables.

C++
enum enum_name { 
    name1, name2, name3, ... 
};

where name1, name2, …. are the names for the constant. They should be unique identifiers. By default, the first name in an enum is assigned the integer value 0, and the subsequent ones are incremented by 1.

After that, we can create a variable of this enum and assign it some name constant that was defined in it.

C++
enum_name var;

Initializing Enum

An enum variable should be initialized by either the name defined in enum definition or directly its integer value.

C++
//Driver Code Starts{
#include <iostream>
using namespace std;

//Driver Code Ends }

// Defining enum
enum direction {
    EAST, NORTH, WEST, SOUTH
};

int main() {

    // Creating enum variable
    direction dir = NORTH;
    cout << dir;

//Driver Code Starts{
    return 0;
}
//Driver Code Ends }

Output
1

Changing Values of Named Constant

We can manually assign values to enum members if needed.

C++
enum enum_name { 
    name1 = val1, name2 = val2, name3, ... 
};

val1, val2 … values should be integer. It is also not compulsory to define the value of all constant names. If the current constant name value is x, then subsequent values will keep incrementing by one.

Example:

C++
//Driver Code Starts{
#include <iostream>
using namespace std;

//Driver Code Ends }

// Defining enum
enum fruit {
    APPLE, BANANA = 5, ORANGE
};

int main() {

    // Creating enum variable
    fruit f = BANANA;
    cout << f << endl;
    
    // Changing the value
    f = ORANGE;

//Driver Code Starts{
    cout << f;
    return 0;
}
//Driver Code Ends }

Output
5
6

Enum Classes

C++11 introduced enum class, which provides better type safety. It helps in resolving name conflicts by providing scope to the constant names. It also requires manual typecasting to integer values from names.

Create enum Class

Enum class can be created just by adding the class keyword in the enum delcaration.

C++
//Driver Code Starts{
#include <iostream>
using namespace std;

//Driver Code Ends }

// Define the enum class
enum class Day { Sunday = 1, Monday, Tuesday,
                Wednesday, Thursday, Friday, 
                Saturday };

int main() {
    
    // initializing
    Day today = Day::Thursday;
    
    // Print the enum
    cout << static_cast<int>(today);

//Driver Code Starts{
    return 0;
}
//Driver Code Ends }

Output
5

If we try to just assign the name of the constant to the enum variable, compiler gives an error.

C++
//Driver Code Starts{
#include <iostream>
using namespace std;

// Define the enum class
enum class Day { Sunday = 1, Monday, Tuesday,
                Wednesday, Thursday, Friday, 
                Saturday };

int main() {
    
//Driver Code Ends }

    // initializing
    Day today = Thursday;

//Driver Code Starts{
    
    // Print the enum
    cout << static_cast<int>(today);
    return 0;
}
//Driver Code Ends }


Output

main.cpp: In function ‘int main()’:
main.cpp:12:17: error: ‘Thursday’ was not declared in this scope; did you mean ‘Day::Thursday’?
12 | Day today = Thursday;
| ^~~~~~~~
| Day::Thursday
main.cpp:6:28: note: ‘Day::Thursday’ declared here
6 | Wednesday, Thursday, Friday,
| ^~~~~~~~


Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg