Learn about Enumerations in C++ Programming


In the previous article, we covered Structures. We have seen that Structures create user-defined data types. Enumerations present a second way to create our own data types. So, let’s learn about Enumerations.

What are Enumerations?
An Enumeration is a custom numeric data type, with a limited set of subsequent values. Each possible value must be given a name (that is why they are limited).

Why Enumerations?
In our life, some names actually have numeric meanings or equivalents. Consider for example the months of the year. January represents the first month in the Julian calendar. When the date is written in a short format, the numeric value of the month is written instead of its name. Similarly, February represents the 2nd month, March is the 3rd, …, and December is the 12th. Another less common example is the days of the week. Consider you are a UNIX or Linux admin, you should know when scheduling a crontab job that days of the week are represented starting from Sunday to Saturday as numbers from 0 to 6 (0 for Sunday, 1 for Monday, etc.). Interestingly, the relation between the Arabic names for the days of the week and their numeric equivalents is clearer. For example, Al-Ahad (that corresponds to Sunday in the western tradition) means one in Arabic, while Al-ithnain (Monday) means two.

So, if this is the case in real life, then why don’t programming languages have something similar?!



By default, the first constant in an enumeration is given the numeric value 0.

The following program creates a custom letter to a number mapping table, which can be used in some types of weak encryptions.

#include <iostream.h>
#include <conio.h>
enum letter
{ h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, a, b, c, d, e, f, g };
void main()
	letter L1, L2;
	L1 = m;
	L2 = c;
	cout << L1 << endl << L2 << endl;

When executed, the program will print the numeric equivalents for the characters m and c.

Letter m, which is the 13th number in the Latin alphabet is now 5, and letter c (which is the 3rd letter) is now equivalent to the 21st letter. The whole alphabet has been shifted to the left by eight positions, so the 8th letter ‘h’ is now the first letter, and corresponds to numeric value of 0. i is 1, j is 2, k is 3, and so on. This way, a word like “wolf” will be translated (ciphered) as “dvsm”.

Learn the Basics of C Programming Language

Specifying the Numeric Values
As I told you, the first constant is given the numeric value 0 by default. That will go fine in most cases. But, what about the Calendar months case?! As we know, January in short date formats will be represented as 1. Similarly, when I say 4/3/1979 (d/m/yyyy format), you will know for sure that I am talking about the 4th day of March, in the year 1979.

Fortunately, Enumerations are kind enough to let us specify an initial value for the first constant (and even for the subsequent constants as well).

The following program defines an enumeration data type called month, containing the Julian month names.

#include <iostream.h>
#include <conio.h>
enum month { January = 1, February, March, April, May, June, July, 
	      August, September, October, November, December };
void main ()
	month m1, m2;
	m1 = March;
	m2 = October;
	cout << m1 << endl << m2 << endl;

This program will print the numeric equivalents for March and October.

In this article, we have learned the second method to create user-defined data types, by using Enumerations.
In the next article, we are going to talk about Arrays. An important topic that you shouldn’t miss. See you there.


Please enter your comment!
Please enter your name here