Enumeration (enum) in C++
An enum (short for enumeration) in C++ is a user-defined data type that consists of a set of named integer constants. It is used to assign meaningful names to integral values, making the code more readable and maintainable.
1️⃣ Syntax of Enum
enum EnumName {
CONSTANT1,
CONSTANT2,
CONSTANT3,
...
};
The default underlying type of enum is int, and constants are assigned values starting from 0 unless explicitly specified.
2️⃣ Basic Example of Enum
#include <iostream>
using namespace std;
enum Color { RED, GREEN, BLUE };
int main() {
Color myColor = GREEN;
cout << "Value of GREEN: " << myColor << endl;
return 0;
}
Output:
Value of GREEN: 1
Explanation:
RED gets 0, GREEN gets 1, and BLUE gets 2.
The variable myColor is assigned GREEN, which corresponds to 1.
3️⃣ Assigning Custom Values to Enum Constants
You can manually assign values to the constants:
#include <iostream>
using namespace std;
enum Size { SMALL = 10, MEDIUM = 20, LARGE = 30 };
int main() {
cout << "SMALL: " << SMALL << endl;
cout << "MEDIUM: " << MEDIUM << endl;
cout << "LARGE: " << LARGE << endl;
return 0;
}
Output:
SMALL: 10
MEDIUM: 20
LARGE: 30
If the first constant has a value, the next constants will increment from it unless explicitly assigned.
4️⃣ Using Enum with Switch Case
Enums are often used with switch statements for better readability.
#include <iostream>
using namespace std;
enum TrafficLight { RED, YELLOW, GREEN };
void checkLight(TrafficLight light) {
switch (light) {
case RED:
cout << "Stop!" << endl;
break;
case YELLOW:
cout << "Get Ready!" << endl;
break;
case GREEN:
cout << "Go!" << endl;
break;
default:
cout << "Invalid Signal!" << endl;
}
}
int main() {
TrafficLight signal = GREEN;
checkLight(signal);
return 0;
}
Output:
Go!
5️⃣ Enum as a Strongly Typed (enum class)
Why Use enum class?
Stronger Type Safety – Prevents implicit conversions to int.
Scoped Constants – Avoids name conflicts with other enums.
Example of enum class
#include <iostream>
using namespace std;
enum class Direction { NORTH, SOUTH, EAST, WEST };
int main() {
Direction dir = Direction::EAST;
if (dir == Direction::EAST) {
cout << "Heading East!" << endl;
}
return 0;
}
Output:
Heading East!
Key Differences Between enum and enum class:
6️⃣ Enum with User Input Example
#include <iostream>
using namespace std;
enum Days { SUNDAY = 1, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY };
int main() {
int choice;
cout << "Enter a number (1-7) for the day of the week: ";
cin >> choice;
Days today = static_cast<Days>(choice);
if (today == SUNDAY || today == SATURDAY) {
cout << "It's the weekend!" << endl;
} else {
cout << "It's a weekday!" << endl;
}
return 0;
}
Output:
Enter a number (1-7) for the day of the week: 6
It's a weekday!
7️⃣ Summary of Enums in C++
✅ Enums improve code readability by replacing numbers with meaningful names.
✅ Default values start at 0, but you can assign custom values.
✅ enum class provides type safety and avoids name conflicts.
✅ Common use cases include menu options, state machines, and configuration settings.
Would you like a real-world project using enums, such as a C++ menu-driven program? 🚀
0 Comments