Introduction to Computer Science - C++

Enumeration Types

In there most basic form enums are sets of variables defined to have different constant integer values. So for example, if enums didn't exist I might created a bunch of constant variables for using in my program:

const int SHEEP = 0;
const int COW =1;
const int FOX =2;
const int TURTLE = 3;
Then I could use these values in my program by using the variable name instead of the value. This will make reading and coding my program simpler. I don't have to remember all the values for the animals but I can simply write the kind of animal and it will mean the number that kind is associated with or defined to be.

int myAnimal;
cin >>myAnimal;
if (myanimal == SHEEP)
I can achieve the exact same result using enums. Using enums in this way not only saves me having to type "const int" but also helps organize the look of my code. Here is how I would do the exact same thing using enum:

enum {
SHEEP = 0,
COW =1,
FOX =2,
Now I have the values SHEEP, COW etc defined as before and I can use them whereever I could use a const int.

The name enum is short for the word enumeration. To enumerate is to assign numerical values to things. Enumerations also allow me to leave out the values and in such a case the compiler will supply values starting from 0. So, the following example is equivalent to the preceding:

enum {
What happens if I run the following code?

cout << TURTLE;
I will see a 3 printed.

I can also tell the compiler to start the enum from a different value from 0. For example, if I want the enum to start from 1 I would write

enum {
SHEEP = 1,
Now if I run the following code I will print a 4 to the screen.

cout << TURTLE;

But this is not all. An enumeration can also be a user defined data type. This can also be called a named enum. The previous examples were unnamed enums. Here is an example of a named enum:

enum animals {
Now I have an enum named animals where each type is defined by a different integer value. That means just like there are the types int, float, char, now there is also a type animal. This type can be assigned the values listed in the enum. I could use it like this:

animals a = animals::FOX;
This means that variable a gets the value of animal associated with COW, i.e. 1.

Can we assign the enum instance the literal integer equivalent of one of the names listed in the declaration? For example, can I also assign it like this?

animals a = 2;
This will work only if the compiler does an implicit cast for me, casting my 2 to type animal. The compiler gcc does this implicit cast for me. But in Visual Studios I need to explicitly cast the number to animal type. Both compilers allow me to compare the value if a enum instance to a literal integer.

Some compilers interpret the C++ language definition to mean we can only assign the enum instance names (or values) listed in the declaration. The compiler gcc allows me to assign completely unrelated values to my enum, as in the example below.

animals a = 17;
I can also do this

animals a = 'a';
Because 'a' is actually the integer value 97;

Here is another example of an enum declaration:


This will create a user define type called Error which can be assign the values DIVIDEBYZERO, NOFILE, ACCESSDENIED, or NORESOURCE where DIVIDEBYZERO == 0, NOFILE == 1, ACCESSDENIED == 2, and NORESOURCE == 3.

Just as with classes, I can create instances of my enumeration by inserting instance identifiers after the closing brace. The following will create 3 instances (a, b and c) of the enum class Error:


By convention, we use all capital letters for values of enums, just as we do with defines. And we capitalize enum names, just like class names.

In order to create an instance of this type I need to write something like this:

Error myerrror;
As long as no other enum or define is using the identifier NORESOURCE, I can give my instance a value like this:
myerror = NORESOURCE;
But if NORESOURCE is also used in another enum or in a define, then I must use the scope resolution operator :: as in the previous example, like this
myerror = Error::NORESOURCE;

By default, the values assigned to an enumeration start at zero (0) and ascend by increments of one. If I want to start at a different number, one (1) for example, I simply need to define the first value to be that number (one) and all following values will increment upward from there. For example, this will give the months of the year the values 1 through 12:


#include "stdio.h"
int main()
if (a == 6 )

    printf("File does not exist.\n");

return 1;

© Nachum Danzig October 2011 - January 2017