Enum

Enum is an enumerator identifier and it can be used to “group” similar, user-defined variables. Every enumeration in C++ is a special integer which is defined as an array of symbolic constants. What this means is that the user can define “special variables” where each variable gets assigned by an unique integer. The point is that when programming, it is easier to work with numerical data, but that kind of data is harder to read. Having a capability to have symbolic representations of certain integer values which are very readable and understandable by the user is the key point you should take away from this data type.

enum vehicle{car, bus, airplane, truck}; // Indexes start from 0!
vehicle myVehicle = car; // "myVehicle" is a variable name which equals to "car", i.e. 0
For example, “airplane” is equal to 2, since every enum type represents one integer value starting from 0 (car = 0, bus = 1, airplane = 2, truck = 3).
Indexes in enum start from 0, but that can be changed.
enum vehicle{car=3, bus, airplane, truck}; // Indexes start from 3 in this case
vehicle myVehicle = truck; // "myVehicle" is a variable name which equals to "truck", i.e. 6

Leave a Reply