Enum Classes in C++ and Their Advantage over Enum DataType
Enums or Enumerated type (enumeration) 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.
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.Enum is a collection of named integer constant means it’s each element is assigned by integer value. 2.It is declared with enum keyword.
C++
#include <iostream> using namespace std; enum roll_no { satya = 70, aakanskah = 73, sanket = 31, aniket = 05, avinash = 68, shreya = 47, nikita = 69, }; int main() { enum roll_no obj; obj = avinash; cout << "The roll no of avinash=" << obj; } |
The roll no of avinash=68
Two enumerations cannot share the same names:
CPP
#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; } |
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; ^
No variable can have a name which is already in some enumeration:
CPP
#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; } |
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, ^
Enums are not type-safe:
CPP
#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; } |
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
CPP
// 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; } |
It's not Red Good people 1
Enumerated types declared the enum class also have more control over their underlying type; it may be any integral data type, such as char, short or unsigned int, which essentially serves to determines the size of the type.
This is specified by a colon and underlying type following the enumerated type:
eg: enum class eyecolor : char {char,green,blue}; Here eyecolor is a distinct type with the same size as a char (1 byte).
C++
#include <iostream> using namespace std; enum rainbow{ violet, indigo, blue, green,yellow,orange,red }colors; enum class eyecolor: char { blue,green,brown }eye; int main() { cout<< "size of enum rainbow variable: " << sizeof (colors)<<endl; cout<< "size of enum class eyecolor variable:" << sizeof (eye)<<endl; return 0; } |
size of enum rainbow variable: 4 size of enum class eyecolor variable:1
Reference: https://en.cppreference.com/w/cpp/language/enum
Contact Us