Enumerated Types or Enums in C++

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

When we assign a float value in a character value then 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.

For example: If a gender variable is created with 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.

Syntax:

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



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. It 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. Beside this, we can create enumerated type variables as same as the normal variables.

    enumerated-type-name variable-name = value;

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


Output:

Gender is Male

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <bits/stdc++.h>
using namespace std;
  
// Defining enum Year
enum year { Jan,
            Feb,
            Mar,
            Apr,
            May,
            Jun,
            Jul,
            Aug,
            Sep,
            Oct,
            Nov,
            Dec };
int main()
{
    int i;
  
    // Traversing the year enum
    for (i = Jan; i <= Dec; i++)
        cout << i << " ";
  
    return 0;
}

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Chasing the dreams

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.