capypad
0 day streak
cpp / beginner
Snippet

Enum Types: Named Constants

Enums create custom data types with named constant values. By default, the first value is 0, but you can assign specific integer values. Enums make code more readable by replacing magic numbers with meaningful names.

snippet.cpp
cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
 
enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY };
enum Priority { LOW = 1, MEDIUM = 5, HIGH = 10 };
 
int main() {
Day today = WEDNESDAY;
Priority p = HIGH;
cout << "Day value: " << today << endl;
cout << "Priority: " << p << endl;
if (today == WEDNESDAY) {
cout << "It's Wednesday!" << endl;
}
return 0;
}
Breakdown
1
enum Day { MONDAY, TUESDAY...
Creates a new type where each name represents an integer value starting at 0
2
Priority { LOW = 1, MEDIUM = 5...
Explicitly assigns values to enum members