Enum Classes in C++ and Their Advantage over Enum DataType

Enums or 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.

Need for Enum Class over Enum Type:
Below are some of the reasons as to what are the limitations of Enum Type and why we need Enum Class to cover them.

  1. Two enumeration cannot share the same names:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
        // Defining enum1 Gender
        enum Gender { Male,
                      Female };
      
        // Defining enum2 Gender2 with same values
        // This will throw error
        enum Gender2 { Male,
                       Female };
      
        // Creating Gender type variable
        Gender gender = Male;
        Gender2 gender2 = Female;
      
        cout << gender << endl
             << gender2;
      
        return 0;
    }

    chevron_right

    
    

    Compilation Error:



    prog.cpp:13:20: error: redeclaration of 'Male'
         enum Gender2 { Male,
                        ^
    prog.cpp:8:19: note: previous declaration 'main()::Gender Male'
         enum Gender { Male,
                       ^
    prog.cpp:14:20: error: redeclaration of 'Female'
                        Female };
                        ^
    prog.cpp:9:19: note: previous declaration 'main()::Gender Female'
                       Female };
                       ^
    prog.cpp:18:23: error: cannot convert 'main()::Gender' 
    to 'main()::Gender2' in initialization
         Gender2 gender2 = Female;
                           ^
    
  2. No variable can have a name which is already in some enumeration:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
        // Defining enum1 Gender
        enum Gender { Male,
                      Female };
      
        // Creating Gender type variable
        Gender gender = Male;
      
        // creating a variable Male
        // this will throw error
        int Male = 10;
      
        cout << gender << endl;
      
        return 0;
    }

    chevron_right

    
    

    Compilation Error:

    prog.cpp: In function 'int main()':
    prog.cpp:16:9: error: 'int Male' redeclared as different kind of symbol
         int Male = 10;
             ^
    prog.cpp:8:19: note: previous declaration 'main()::Gender Male'
         enum Gender { Male,
                       ^
    
  3. Enums are not type safe:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
        // Defining enum1 Gender
        enum Gender { Male,
                      Female };
      
        // Defining enum2 Color
        enum Color { Red,
                     Green };
      
        // Creating Gender type variable
        Gender gender = Male;
        Color color = Red;
      
        // Upon comparing gender and color
        // it will return true as both have value 0
        // which should not be the case actually
        if (gender == color)
            cout << "Equal";
      
        return 0;
    }

    chevron_right

    
    

    Warning:

    prog.cpp: In function 'int main()':
    prog.cpp:23:19: warning: comparison between 'enum main()::Gender'
    and 'enum main()::Color' [-Wenum-compare]
         if (gender == color)                ^
    

Enum Class

C++11 has introduced enum classes (also called scoped enumerations), that makes enumerations both strongly typed and strongly scoped. Class enum doesn’t allow implicit conversion to int, and also doesn’t compare enumerators from different enumerations.

To define enum class we use class keyword after enum keyword.
Syntax:

// Declaration
enum class EnumName{ Value1, Value2, ... ValueN};

// Initialisation
EnumName ObjectName = EnumName::Value;

Example:

// Declaration
enum class Color{ Red, Green, Blue};

// Initialisation
Color col = Color::Red;

Below is an implementation to show Enum Class

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to demonstrate working
// of Enum Classes
  
#include <iostream>
using namespace std;
  
int main()
{
  
    enum class Color { Red,
                       Green,
                       Blue };
    enum class Color2 { Red,
                        Black,
                        White };
    enum class People { Good,
                        Bad };
  
    // An enum value can now be used
    // to create variables
    int Green = 10;
  
    // Instantiating the Enum Class
    Color x = Color::Green;
  
    // Comparison now is completely type-safe
    if (x == Color::Red)
        cout << "It's Red\n";
    else
        cout << "It's not Red\n";
  
    People p = People::Good;
  
    if (p == People::Bad)
        cout << "Bad people\n";
    else
        cout << "Good people\n";
  
    // gives an error
    // if(x == p)
    // cout<<"red is equal to good";
  
    // won't work as there is no
    // implicit conversion to int
    // cout<< x;
  
    cout << int(x);
  
    return 0;
}

chevron_right


Output:

It's not Red
Good people
1

Reference: https://en.cppreference.com/w/cpp/language/enum



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : aryanrawlani007



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.