Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
🔵 Core C++  ·  Lesson 29

Enumerations in C++

Written and reviewed by · 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

C++
#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;
}
Output:
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:

C++
enum Level { Low = 1, Medium = 5, High };   // High = 6

Enums in switch

Enums pair naturally with switch:

C++
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;
}
Output:
Go

enum class (scoped)

Modern C++ prefers enum class, which keeps names scoped and does not silently turn into int:

C++
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 → 1

The Color:: prefix means two different scoped enums can both have a Red without clashing.

When to use which

Use enum class whenPlain enum is OK when
You want safety and no name clashesQuick code, small scope
You do not want int conversionYou rely on the int value directly

As a rule, prefer enum class in new code.

Common mistakes

  • Forgetting the Color:: prefix with an enum class.
  • Expecting a plain enum's names to be scoped — they leak into the surrounding scope.
  • Assuming an enum class converts to int automatically (it needs a cast).
  • Reusing a name across two plain enums, causing a clash.
🏋️ Practice

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 switch statements.
  • enum class is scoped and does not implicitly convert to int.
  • Prefer enum class in modern C++ for safety.

enum क्या है?

enum (enumeration) संबंधित पूर्ण-संख्या values के समूह को सरल नाम देता है। यह याद रखने के बजाय कि 0 का मतलब Monday और 1 का मतलब Tuesday है, आप उन्हें नाम देते हैं — code सादे अंग्रेज़ी जैसा पढ़ा जाता है और गलतियाँ पकड़ना आसान होता है।

एक सादा enum

C++
#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;
}
Output:
Day number: 2

Default रूप से पहला नाम 0 है, अगला 1, और ऐसे ही — तो Wed 2 है।

Custom values

आप अपने numbers सेट कर सकते हैं; unset वाले आख़िरी से जारी रहते हैं:

C++
enum Level { Low = 1, Medium = 5, High };   // High = 6

switch में enums

Enums switch से स्वाभाविक रूप से जुड़ते हैं:

C++
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;
}
Output:
Go

enum class (scoped)

आधुनिक C++ enum class पसंद करता है, जो नाम scoped रखता है और चुपचाप int में नहीं बदलता:

C++
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 → 1

Color:: 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 switch statements में साफ़ काम करते हैं।
  • enum class scoped है और अंतर्निहित रूप से int में नहीं बदलता।
  • सुरक्षा के लिए आधुनिक C++ में enum class पसंद करें।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C++ में enum क्या है?
enum, enumeration का संक्षिप्त रूप, एक type है जो संबंधित integer values के समूह को पढ़ने योग्य नाम देता है। जैसे 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 करते हैं?
आप definition में = से 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 इस्तेमाल कर सकते हैं?
हाँ, enums switch में स्वाभाविक रूप से काम करते हैं क्योंकि वे integer-आधारित हैं। आप हर enum value के लिए एक case लिखते हैं, जो raw numbers इस्तेमाल करने से साफ़ है और compiler को आपको चेताने देता है अगर आप कोई value संभालना भूलें।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.