Enumerations in C++
Written and reviewed by Gagan Bhardwaj · Senior IT Faculty · 15+ years’ experience
What is an enum?
An enum (enumeration) gives friendly names to a set of related whole-number values. Instead of remembering that 0 means Monday and 1 means Tuesday, you name them — the code reads like plain English and mistakes are easier to spot.
A plain enum
#include <iostream>
using namespace std;
enum Day { Mon, Tue, Wed, Thu, Fri };
int main() {
Day today = Wed;
cout << "Day number: " << today << "\n"; // 2
return 0;
}Day number: 2
By default the first name is 0, the next 1, and so on — so Wed is 2.
Custom values
You can set your own numbers; unset ones continue from the last:
enum Level { Low = 1, Medium = 5, High }; // High = 6Enums in switch
Enums pair naturally with switch:
enum Signal { Red, Yellow, Green };
Signal s = Green;
switch (s) {
case Red: cout << "Stop\n"; break;
case Yellow: cout << "Wait\n"; break;
case Green: cout << "Go\n"; break;
}Go
enum class (scoped)
Modern C++ prefers enum class, which keeps names scoped and does not silently turn into int:
enum class Color { Red, Green, Blue };
Color c = Color::Green; // must qualify with Color::
// int n = c; // ERROR: no implicit conversion
int n = (int)c; // explicit cast is allowed → 1The Color:: prefix means two different scoped enums can both have a Red without clashing.
When to use which
| Use enum class when | Plain enum is OK when |
|---|---|
| You want safety and no name clashes | Quick code, small scope |
| You do not want int conversion | You rely on the int value directly |
As a rule, prefer enum class in new code.
Common mistakes
- Forgetting the
Color::prefix with anenum class. - Expecting a plain enum's names to be scoped — they leak into the surrounding scope.
- Assuming an
enum classconverts to int automatically (it needs a cast). - Reusing a name across two plain enums, causing a clash.
Define an enum class Direction { North, East, South, West }. Write a function that takes a Direction and prints its name using a switch. Call it for each direction.
Summary
- An enum names a set of related integer values for readable code.
- Values start at 0 by default; you can set custom numbers.
- Enums work cleanly in
switchstatements. enum classis scoped and does not implicitly convert to int.- Prefer
enum classin modern C++ for safety.
Frequently Asked Questions
What is an enum in C++?
enum Day { Mon, Tue, Wed }; lets you use Mon instead of a magic number, making code clearer and less error-prone.What is the difference between enum and enum class in C++?
enum puts its names in the surrounding scope and converts freely to int, which can cause clashes and bugs. An enum class (scoped enum) keeps its names inside the enum's scope and does not implicitly convert to int, making it safer and preferred in modern C++.How do you assign specific values to enum constants in C++?
= in the definition, such as enum Level { Low = 1, Medium = 5, High = 10 };. Any constant you do not assign takes the previous value plus one, so numbering continues automatically from the last set value.Why is enum class preferred over plain enum in C++?
enum class avoids two problems of plain enums: its names do not leak into the surrounding scope, so different enums can reuse names, and it does not silently convert to int, which prevents accidental misuse. This makes code safer and clearer.Can you use an enum in a switch statement in C++?
switch because they are integer-based. You write a case for each enum value, which is clearer than using raw numbers and lets the compiler warn you if you forget to handle a value.enum क्या है?
enum (enumeration) संबंधित पूर्ण-संख्या values के समूह को सरल नाम देता है। यह याद रखने के बजाय कि 0 का मतलब Monday और 1 का मतलब Tuesday है, आप उन्हें नाम देते हैं — code सादे अंग्रेज़ी जैसा पढ़ा जाता है और गलतियाँ पकड़ना आसान होता है।
एक सादा enum
#include <iostream>
using namespace std;
enum Day { Mon, Tue, Wed, Thu, Fri };
int main() {
Day today = Wed;
cout << "Day number: " << today << "\n"; // 2
return 0;
}Day number: 2
Default रूप से पहला नाम 0 है, अगला 1, और ऐसे ही — तो Wed 2 है।
Custom values
आप अपने numbers सेट कर सकते हैं; unset वाले आख़िरी से जारी रहते हैं:
enum Level { Low = 1, Medium = 5, High }; // High = 6switch में enums
Enums switch से स्वाभाविक रूप से जुड़ते हैं:
enum Signal { Red, Yellow, Green };
Signal s = Green;
switch (s) {
case Red: cout << "Stop\n"; break;
case Yellow: cout << "Wait\n"; break;
case Green: cout << "Go\n"; break;
}Go
enum class (scoped)
आधुनिक C++ enum class पसंद करता है, जो नाम scoped रखता है और चुपचाप int में नहीं बदलता:
enum class Color { Red, Green, Blue };
Color c = Color::Green; // Color:: se qualify karna zaroori
// int n = c; // ERROR: koi implicit conversion nahi
int n = (int)c; // explicit cast allowed hai → 1Color:: prefix का मतलब दो अलग scoped enums दोनों में बिना टकराव Red हो सकता है।
कौन-सा कब इस्तेमाल करें
| enum class तब जब | सादा enum ठीक तब जब |
|---|---|
| आप सुरक्षा और कोई नाम टकराव न चाहें | त्वरित code, छोटा scope |
| आप int conversion न चाहें | आप int value पर सीधे निर्भर हों |
नियम के रूप में, नए code में enum class पसंद करें।
आम गलतियाँ
enum classके साथColor::prefix भूलना।- सादे enum के नामों को scoped मानना — वे आसपास के scope में रिसते हैं।
- यह मानना कि
enum classअपने आप int में बदलता है (इसे cast चाहिए)। - दो सादे enums में एक नाम दोबारा इस्तेमाल करना, टकराव पैदा करना।
एक enum class Direction { North, East, South, West } परिभाषित करें। एक function लिखें जो Direction ले और switch से उसका नाम print करे। हर direction के लिए इसे call करें।
सारांश
- enum पढ़ने योग्य code के लिए संबंधित integer values के समूह को नाम देता है।
- Values default रूप से 0 से शुरू होती हैं; आप custom numbers सेट कर सकते हैं।
- Enums
switchstatements में साफ़ काम करते हैं। enum classscoped है और अंतर्निहित रूप से int में नहीं बदलता।- सुरक्षा के लिए आधुनिक C++ में
enum classपसंद करें।