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.

Frequently Asked Questions

What is an enum in C++?
An enum, short for enumeration, is a type that gives readable names to a set of related integer values. For example 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++?
A plain 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++?
You assign values with = 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++?
Yes, enums work naturally in a 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

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 पसंद करें।
← Back to C++ Tutorial
🔗

Share this topic with a friend

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

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

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

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.