Enumerations

Jaskomal
1 min readJan 28, 2021

Commonly called enums in C++ make it easy to assign names to values. This helps make code cleaner and easier to read, and the key in programming is that you’re not making code for yourself, but others.

Enums are declared as a list of constants an example of this would be months of a year:

enum Months {JAN = 31, FEB = 28, MAR = 31, APR = 30, MAY = 31, JUN = 30, JUL = 31, AUG = 31, SEP = 30, OCT = 31, NOV = 30, DEC = 31};

In enums multiple named constants can have the same integer value. As shown above.

My personal favorite feature of enums is that it assigns values in order so you don’t have to go through and add integer value if you are creating a list in numerical order.

enum Numbers {ONE, TWO, THREE, FOUR, FIVE};

Is the same as

enum Numbers {ONE = 1, TWO = 2, THREE = 3, FOUR = 4, FIVE = 5};

If you assign values to a few constants, enums will assign the next value to constants so you don’t have to.

enum Partials {A = 5, B, C, E = -4, F};

In the example above the value of B will be 6, C = 7, and F = -3. If you don’t want your enum to act as an integer you can use strong enum/ enum classes but we will save that for another day. This has been your super basic overview of enums.

--

--