Enum in C++ is a data type that holds a fixed collection of constants.
It may be used to represent days of the week (SUNDAY, MONDAY, TUESDAY, WEDNESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY) as well as directions (NORTH, SOUTH, EAST, and WEST). The enum constants in C++ are implicitly static and final.
Enums are classes with a collection of constants that cannot be changed.
Points to Keep in Mind When Using C++ Enum:
- Type safety is improved using enum.
- Enums are simple to use in switches, and they can be traversed. They can also contain fields, constructors, and methods.
- Enum can implement a lot of interfaces, but it can’t extend any class because it extends itself. Enumeration class.
Example of enumeration in C++:
Let’s look at a basic enum data type example in a C++ application.
#include <iostream> using namespace std; enum week { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }; int main() { week day; day = Friday; cout << "Day: " << day+1<<endl; return 0; }
Output:
Day: 5