-
Hajipur, Bihar, 844101
In C++, an enum (short for enumeration) is a user-defined data type that allows you to assign names to integral constants. It helps make your code more readable and organized, especially when dealing with a fixed set of related values like days of the week, directions, or months.
Enums are a great way to represent named constants instead of using plain numbers in your code.
An enum is a collection of symbolic names (called enumerators) that represent integer values.
It provides a way to define a set of constants under one type name.
enum Day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
Here:
Day is the name of the enumeration.
Sunday, Monday, etc. are the enumerator constants.
By default, the first value (Sunday) is assigned 0, and each following one increments by 1.
So internally:
Sunday = 0
Monday = 1
Tuesday = 2
Wednesday = 3
Thursday = 4
Friday = 5
Saturday = 6
Enums are declared using the enum keyword.
The general syntax looks like this:
enum enumName {
value1,
value2,
value3,
...
};
You can then declare variables of that enum type and assign values.
#include <iostream>
using namespace std;
enum Color { Red, Green, Blue };
int main() {
Color c1 = Red;
cout << "Color value: " << c1;
return 0;
}
Output:
Color value: 0
Even though you see Red, the program actually stores it as the integer 0.
You can assign custom integer values to any enumerator.
Subsequent values increase automatically unless explicitly defined.
#include <iostream>
using namespace std;
enum Status { Pending = 1, Approved = 3, Rejected = 5 };
int main() {
Status s = Approved;
cout << "Status: " << s;
return 0;
}
Output:
Status: 3
If you assign one value manually, the next enumerators continue from that number unless given another specific value.
Enums work well with conditional logic like if, switch, and for statements.
#include <iostream>
using namespace std;
enum Direction { North, South, East, West };
int main() {
Direction dir = East;
switch (dir) {
case North:
cout << "Moving North";
break;
case South:
cout << "Moving South";
break;
case East:
cout << "Moving East";
break;
case West:
cout << "Moving West";
break;
}
return 0;
}
Output:
Moving East
This makes programs more readable because you use meaningful names instead of raw numbers.
You can assign and compare enum variables directly since they represent integers internally.
Example:
enum Level { Low, Medium, High };
int main() {
Level l1 = Medium;
Level l2 = High;
if (l2 > l1)
cout << "High level is greater than Medium.";
return 0;
}
Output:
High level is greater than Medium.
Here, Medium is internally 1 and High is 2, so the comparison works numerically.
By default, traditional enums in C++ are not strongly typed.
That means you can mix them with integers, which may lead to unexpected results in large programs.
Example of potential issue:
enum Fruit { Apple, Mango };
int x = Apple;
x = 10; // Works, but not ideal
To solve this, C++11 introduced scoped enums (also known as enum class).
Scoped enums provide strong typing and better control.
You must use the enum name to access its members, avoiding naming conflicts.
#include <iostream>
using namespace std;
enum class TrafficLight { Red, Yellow, Green };
int main() {
TrafficLight light = TrafficLight::Green;
if (light == TrafficLight::Green)
cout << "Go!";
else
cout << "Stop!";
return 0;
}
Output:
Go!
| Feature | Traditional Enum | Enum Class |
|---|---|---|
| Type Safety | Weak (can mix with int) | Strong (cannot mix with int) |
| Scope | Global | Scoped |
| Syntax | Red |
TrafficLight::Red |
| Implicit Conversion | Allowed | Not allowed |
By default, enums are stored as int. But you can specify another underlying type using a colon (:).
enum class ErrorCode : char { Success = 'S', Failure = 'F' };
This gives more flexibility when dealing with memory optimization or special representations.
Enums can be used with loops to iterate through a sequence of values.
Example:
#include <iostream>
using namespace std;
enum Weekday { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
int main() {
for (int i = Sun; i <= Sat; i++) {
cout << i << " ";
}
return 0;
}
Output:
0 1 2 3 4 5 6
Although enums store names, they behave like integers in such cases.
By default, enums cannot directly take input as strings or display their names as output.
You can handle this by using arrays or switch statements to map between strings and enum values.
Example:
#include <iostream>
using namespace std;
enum Month { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec };
int main() {
int num;
cout << "Enter month number (0–11): ";
cin >> num;
Month m = static_cast<Month>(num);
switch (m) {
case Jan: cout << "January"; break;
case Feb: cout << "February"; break;
case Mar: cout << "March"; break;
default: cout << "Other month"; break;
}
return 0;
}
Output:
Enter month number (0–11): 2
March
Improves readability – names are easier to understand than numbers.
Organizes constants in one place.
Prevents invalid values (only defined constants can be used).
Easier debugging since you know what each constant represents.
Scoped enums add strong type safety in modern C++.
An enum in C++ is a convenient way to group related named constants.
It replaces hard-coded integers with readable identifiers.
You can use traditional enums for simple tasks and enum class for type-safe programming.
Enums improve code organization, readability, and maintainability — especially in larger projects.
Write a program to create an enum Days for all seven days of the week. Take user input as a number (0–6) and display the corresponding day name.
Define an enum TrafficSignal with values Red, Yellow, and Green. Write a program that prints “Stop”, “Wait”, or “Go” based on the signal value.
Create an enum Month for all 12 months. Ask the user for a month number and display how many days it has (use switch statement).
Write a program to demonstrate assigning custom integer values in an enum ErrorCode with members Success = 200, NotFound = 404, and ServerError = 500.
Create an enum Weather with members Sunny, Rainy, and Cloudy. Use an if-else structure to display weather-related messages.
Define an enum class Level { Low, Medium, High }. Accept an integer input and display the matching level using static_cast.
Write a program that uses an enum Direction and a switch statement to display “Moving North”, “Moving South”, “Moving East”, or “Moving West”.
Create an enum Grades with values A, B, C, D, F. Ask the user to enter marks and display the grade using the enum.
Write a program that demonstrates how to iterate over enum values (using a for loop and integer typecasting).
Create a scoped enum (enum class) named Permission with values Read, Write, and Execute. Print all permissions one by one using a loop.