C++ enum Keyword
The enum
keyword in C++ is used to define an enumeration, a user-defined data type that consists of a set of named integral constants. Enumerations improve code readability by providing meaningful names to values, reducing the risk of errors from using plain integers.
Starting from C++11, scoped enumerations (using enum class
) were introduced, offering better type safety and scoping compared to traditional enumerations.
Syntax
</>
Copy
enum EnumName {
constant1,
constant2,
constant3,
...
};
enum class EnumName {
constant1,
constant2,
constant3,
...
};
- enum
- The keyword used to define a traditional enumeration.
- enum class
- The keyword used to define a scoped enumeration with better type safety.
- EnumName
- The name of the enumeration type.
- constant1, constant2, constant3, …
- The named constants within the enumeration, each representing an integral value.
Examples
Example 1: Traditional Enumeration
This example demonstrates the use of a traditional enum
to define a set of constants representing weekdays.
</>
Copy
#include <iostream>
using namespace std;
enum Weekday {
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
};
int main() {
Weekday today = Wednesday;
if (today == Wednesday) {
cout << "It's Wednesday!" << endl;
}
return 0;
}
Output:
It's Wednesday!
Explanation:
- The
Weekday
enumeration defines constants for the days of the week. - The variable
today
is assigned the valueWednesday
. - The program checks if
today
is equal toWednesday
and prints the corresponding message.
Example 2: Scoped Enumeration
This example demonstrates the use of enum class
to define a scoped enumeration for traffic light states.
</>
Copy
#include <iostream>
using namespace std;
enum class TrafficLight {
Red,
Yellow,
Green
};
int main() {
TrafficLight light = TrafficLight::Green;
if (light == TrafficLight::Green) {
cout << "Go!" << endl;
}
return 0;
}
Output:
Go!
Explanation:
- The
TrafficLight
scoped enumeration defines constants for traffic light states. - The variable
light
is assigned the valueTrafficLight::Green
. - The program checks if
light
is equal toTrafficLight::Green
and prints “Go!”.
Example 3: Assigning Custom Values
This example shows how to assign custom integer values to enumeration constants.
</>
Copy
#include <iostream>
using namespace std;
enum ErrorCode {
Success = 0,
NotFound = 404,
ServerError = 500
};
int main() {
ErrorCode code = NotFound;
cout << "Error code: " << code << endl;
return 0;
}
Output:
Error code: 404
Explanation:
- The
ErrorCode
enumeration assigns custom integer values to its constants. - The variable
code
is assigned the valueNotFound
, which corresponds to404
. - The program prints the integer value of
code
.
Key Points about enum
Keyword
- The
enum
keyword defines a traditional enumeration, whileenum class
defines a scoped enumeration. - Enumerations assign meaningful names to integral values, improving code readability.
- Scoped enumerations (
enum class
) provide better type safety and require fully qualified names (e.g.,TrafficLight::Green
). - Enumeration constants can have custom integer values.
- Enumerations are particularly useful for representing states, modes, or fixed sets of options.