Open In App

Enumeration in C++

Last Updated : 05 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Enumeration (Enumerated type) is a user-defined data type that can be assigned some limited values. These values are defined by the programmer at the time of declaring the enumerated type.

If we assign a float value to a character value, then the compiler generates an error. In the same way, if we try to assign any other value to the enumerated data types, the compiler generates an error. Enumerator types of values are also known as enumerators. It is also assigned by zero the same as the array. It can also be used with switch statements.

Syntax:

enum enumerated-type-name
{
    value1, value2, value3…..valueN
};

For Example: If a gender variable is created with the value male or female. If any other value is assigned other than male or female then it is not appropriate. In this situation, one can declare the enumerated type in which only male and female values are assigned.

Enum in C++

The enum keyword is used to declare enumerated types after that enumerated type name was written then under curly brackets possible values are defined. After defining Enumerated type variables are created. 
Enumerators can be created in two types:-

  1. It can be declared during declaring enumerated types, just add the name of the variable before the semicolon. or,
  2. Besides this, we can create enumerated type variables as the same as the normal variables.
enumerated-type-name variable-name = value;

By default, the starting code value of the first element of the enum is 0 (as in the case of the array). But it can be changed explicitly.

Example:

enum enumerated-type-name{value1=1, value2, value3};

Also, The consecutive values of the enum will have the next set of code value(s).

Example:

//first_enum is the enumerated-type-name
enum first_enum{value1=1, value2=10, value3};

In this case, 
first_enum e;
e=value3;
cout<<e;

Output:
11

Example:

C++




// C++ Program to Demonstrate the Functioning of Enumerators
// with an example of Gender
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // Defining enum Gender
    enum Gender { Male, Female };
  
    // Creating Gender type variable
    Gender gender = Male;
  
    switch (gender) {
    case Male:
        cout << "Gender is Male";
        break;
    case Female:
        cout << "Gender is Female";
        break;
    default:
        cout << "Value can be Male or Female";
    }
    return 0;
}


Output: 

Gender is Male

 

Example:

C++




// C++ Program to Demonstrate the Functioning of Enumerators
// with an Example of Year
#include <bits/stdc++.h>
using namespace std;
  
// Defining enum Year
enum year {
    Jan,
    Feb,
    Mar,
    Apr,
    May,
    Jun,
    Jul,
    Aug,
    Sep,
    Oct,
    Nov,
    Dec
};
  
// Driver Code
int main()
{
    int i;
  
    // Traversing the year enum
    for (i = Jan; i <= Dec; i++)
        cout << i << " ";
  
    return 0;
}


Output: 

0 1 2 3 4 5 6 7 8 9 10 11

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments