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;
}
|
Output
The roll no of avinash=68
Two enumerations cannot share the same names:
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
enum Gender { Male,
Female };
enum Gender2 { Male,
Female };
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()
{
enum Gender { Male,
Female };
Gender gender = Male;
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()
{
enum Gender { Male,
Female };
enum Color { Red,
Green };
Gender gender = Male;
Color color = Red;
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
#include <iostream>
using namespace std;
int main()
{
enum class Color { Red,
Green,
Blue };
enum class Color2 { Red,
Black,
White };
enum class People { Good,
Bad };
int Green = 10;
Color x = Color::Green;
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" ;
cout << int (x);
return 0;
}
|
Output
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;
}
|
Output
size of enum rainbow variable: 4
size of enum class eyecolor variable:1
Reference: https://en.cppreference.com/w/cpp/language/enum
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
27 Dec, 2022
Like Article
Save Article