Enumerations

Back

Loading concept...

๐ŸŽจ The Label Makerโ€™s Magic: C++ Enumerations


๐ŸŒŸ Once Upon a Time in Label Landโ€ฆ

Imagine you have a magical label maker. Instead of remembering numbers like โ€œ1 means Red, 2 means Blue, 3 means Green,โ€ you just write the actual words!

Thatโ€™s exactly what enumerations do in C++. They let you create named labels so your code is easy to read and understand.


๐Ÿ“ฆ Chapter 1: Plain Enumerations (The Classic Labels)

What Are They?

Think of a plain enum like a crayon box where each crayon has a name AND a hidden number.

enum Color {
    Red,    // secretly = 0
    Green,  // secretly = 1
    Blue    // secretly = 2
};

How to Use Them

Color myFavorite = Red;

if (myFavorite == Red) {
    // Do something red!
}

๐ŸŽฏ The Hidden Numbers

Each label gets a number automatically, starting from 0:

enum Day {
    Monday,    // 0
    Tuesday,   // 1
    Wednesday  // 2
};

You can also pick your own numbers:

enum Level {
    Easy = 1,
    Medium = 5,
    Hard = 10
};

โš ๏ธ The Problem with Plain Enums

Plain enums are a bit messy. They spill their names everywhere!

enum Fruit { Apple, Orange };
enum Tech { Apple, Windows };
// ERROR! Two "Apple" names clash!

This is like having two kids named โ€œAlexโ€ in the same classroom. Confusing!


๐Ÿฐ Chapter 2: Enum Classes (The Safe Labels)

The Super-Safe Solution

C++11 introduced enum class โ€” think of it as putting each label set in its own labeled box.

enum class Fruit { Apple, Orange };
enum class Tech { Apple, Windows };
// No problem! Each Apple is in its own box!

How to Use Enum Classes

You must say which box the label comes from:

Fruit mySnack = Fruit::Apple;
Tech myDevice = Tech::Apple;

// Clear and safe!
if (mySnack == Fruit::Apple) {
    // Yummy!
}

๐Ÿ”’ Why Enum Classes Are Better

Feature Plain enum enum class
Names can clash โœ… Yes (bad!) โŒ No (safe!)
Needs box name โŒ No โœ… Yes
Converts to number easily โœ… Yes โŒ No (intentional)

Converting to Numbers (When You Need To)

enum class Size { Small, Medium, Large };

Size shirt = Size::Medium;

// Need the number? Ask explicitly:
int num = static_cast<int>(shirt);
// num is now 1

๐Ÿช„ Chapter 3: Using Enum (The Shortcut Spell)

The Magic Words: using enum

Tired of typing the box name every time? C++20 gives you a shortcut!

enum class Direction {
    Up, Down, Left, Right
};

void move() {
    using enum Direction;

    // Now you can just say:
    Direction d = Up;  // No need for Direction::Up

    if (d == Left) {
        // Turn left!
    }
}

๐ŸŽฎ Real Example: Game Controls

enum class Button {
    A, B, X, Y, Start, Select
};

void handleInput(Button pressed) {
    using enum Button;

    switch (pressed) {
        case A:     jump();    break;
        case B:     attack();  break;
        case X:     defend();  break;
        case Y:     special(); break;
        case Start: pause();   break;
        case Select: menu();   break;
    }
}

โš ๏ธ Use With Care!

using enum brings all names into your current scope. Use it inside functions to keep things tidy.


๐Ÿ—บ๏ธ The Complete Picture

graph TD A["Enumerations"] --> B["Plain enum"] A --> C["enum class"] A --> D["using enum"] B --> B1["Names visible everywhere"] B --> B2["Can clash with other names"] B --> B3["Auto-converts to int"] C --> C1["Names in their own scope"] C --> C2["No name clashes"] C --> C3["Must use ScopeName::Value"] D --> D1["C++20 shortcut"] D --> D2["Brings names into scope"] D --> D3["Use inside functions"]

๐Ÿ’ก Quick Tips

When to Use What?

Situation Use This
Simple, local use Plain enum
Avoiding name clashes enum class
Less typing with enum class using enum
Working with old C code Plain enum
New modern C++ code enum class โœจ

๐ŸŽจ Set Your Own Type

Both can specify their underlying number type:

enum class SmallSize : uint8_t {
    Tiny,
    Small,
    Medium
};
// Uses only 1 byte!

๐ŸŽ‰ You Did It!

You now know:

  1. โœ… Plain enums โ€” Simple labels with hidden numbers
  2. โœ… Enum classes โ€” Safe labels in their own boxes
  3. โœ… Using enum โ€” The shortcut to avoid typing

Enumerations make your code readable, safe, and meaningful. No more mysterious numbers!


๐Ÿง™โ€โ™‚๏ธ Pro Wisdom: Always prefer enum class in modern C++. Itโ€™s like wearing a seatbelt โ€” a small habit that prevents big problems!

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.