Enumeration
🟢 Step 1: What is an Enum?
👉 Enum (short for Enumeration) means
giving names to numbers — so your code becomes easy to read and understand.
It’s a user-defined data type that allows you to assign names to integer values.
🔸 Without enum (normal way):
int day = 3; // 3 means Wednesday
Can anyone guess what “3” means here? 🤔
No! It could be any day — hard to understand.
🔹 With enum:
enum Week { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };
Now you can write:
Week today = Wednesday;
Now it’s clear that today is Wednesday — not just a random number.
🔹 Internally how it works:
C++ automatically gives numbers (starting from 0):
| Name | Value |
| Monday | 0 |
| Tuesday | 1 |
| Wednesday | 2 |
| Thursday | 3 |
| Friday | 4 |
| Saturday | 5 |
| Sunday | 6 |
🟢 Step 2: A simple example
#include <iostream>
using namespace std;
enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
int main() {
Days today = Monday;
if (today == Sunday) {
cout << "Today is Sunday — it's a holiday!" << endl;
} else {
cout << "Today is a working day!" << endl;
}
return 0;
}
🧠 Output:
Today is a working day!
🟢 Step 3: What happens here
enumcreates a list of names (Sunday, Monday, …)C++ replaces them with numbers (0, 1, 2, …)
You can use the names instead of numbers → code becomes readable and meaningful
🟢 Step 4: Why do we use enum?
| Problem | Enum Fixes It |
| Numbers are confusing | Names are clear |
| Hard to remember what 0 or 1 means | Easy to remember names |
| Code looks messy | Code looks neat and readable |
🟢 Step 5: Real-life Example — Traffic Light 🚦
#include <iostream>
using namespace std;
enum Light { Red, Yellow, Green };
int main() {
Light signal = Red;
if (signal == Red)
cout << "STOP!" << endl;
else if (signal == Yellow)
cout << "READY!" << endl;
else
cout << "GO!" << endl;
return 0;
}
🧠 Output:
STOP!
🟩 In one simple line:
“Enum is a list of names that represent numbers — making the code easier to understand.”
Example:
enum Days { Sunday, Monday, Tuesday };
✅ Summary:
| Feature | Description |
| Type | User-defined |
| Starts from | 0 by default |
| Purpose | Replaces confusing numbers with readable names |
| Example | enum Color { Red, Green, Blue }; |
| Use | Makes code simple and meaningful |