Enum is a data type defined in CPP library that can include fixed set of static and implicit constants like days of the week, directions etc.
Advantages of using Enum:
- Improves type safety.
- Can be easily used in switch.
- Can be traversed.
- Can have fields, constructors and methods.
- May implement many interfaces but cannot extend any class because it internally extends Enum class.
Example:
#include <iostream.h> using namespace std; enum directions { North, South, East, West }; int main() { directions direction; direction = East; cout << "Direction: " << direction+1 <<endl; return 0; } |
Output
Direction: 3 |