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.
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पसंद करें।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
C++ में enum क्या है?
enum Day { Mon, Tue, Wed }; आपको magic number के बजाय Mon इस्तेमाल करने देता है, code को साफ़ और कम त्रुटि-प्रवण बनाते हुए।C++ में enum और enum class में क्या अंतर है?
enum अपने नाम आसपास के scope में रखता है और स्वतंत्र रूप से int में बदलता है, जो टकराव और bugs पैदा कर सकता है। enum class (scoped enum) अपने नाम enum के scope के अंदर रखता है और अंतर्निहित रूप से int में नहीं बदलता, इसे सुरक्षित और आधुनिक C++ में पसंदीदा बनाते हुए।C++ में enum constants को विशिष्ट values कैसे assign करते हैं?
= से values assign करते हैं, जैसे enum Level { Low = 1, Medium = 5, High = 10 };। जो constant आप assign नहीं करते वह पिछली value plus एक लेता है, तो numbering आख़िरी सेट value से अपने आप जारी रहती है।C++ में सादे enum के बजाय enum class क्यों पसंद किया जाता है?
enum class सादे enums की दो समस्याएँ टालता है: इसके नाम आसपास के scope में नहीं रिसते, तो अलग enums नाम दोबारा इस्तेमाल कर सकते हैं, और यह चुपचाप int में नहीं बदलता, जो आकस्मिक दुरुपयोग रोकता है। यह code को सुरक्षित और साफ़ बनाता है।क्या C++ में switch statement में enum इस्तेमाल कर सकते हैं?
switch में स्वाभाविक रूप से काम करते हैं क्योंकि वे integer-आधारित हैं। आप हर enum value के लिए एक case लिखते हैं, जो raw numbers इस्तेमाल करने से साफ़ है और compiler को आपको चेताने देता है अगर आप कोई value संभालना भूलें।