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:
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.
const int SHEEP = 0;
const int COW =1;
const int FOX =2;
const int TURTLE = 3;
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:
int myAnimal;
cin >>myAnimal;
if (myanimal == SHEEP)
etc....
Now I have the values SHEEP, COW etc defined as before and I can use them whereever I could use a const int.
enum {
SHEEP = 0,
COW =1,
FOX =2,
TURTLE = 3,
};
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:
What happens if I run the following code?
enum {
SHEEP,
COW,
FOX,
TURTLE ,
};
I will see a 3 printed.
cout << TURTLE;
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
Now if I run the following code I will print a 4 to the screen.
enum {
SHEEP = 1,
COW,
FOX,
TURTLE ,
};
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:
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:
enum animals {
SHEEP,
COW,
FOX,
TURTLE ,
};
This means that variable a gets the value of animal associated with COW, i.e. 1.
animals a = animals::FOX;
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?
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.
animals a = 2;
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.
I can also do this
animals a = 17;
Because 'a' is actually the integer value 97;
animals a = 'a';
Here is another example of an enum declaration:
enum Error {DIVIDEBYZERO, NOFILE, ACCESSDENIED, NORESOURCE};
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:
enum Error {DIVIDEBYZERO, NOFILE, ACCESSDENIED, NORESOURCE}a,b,c;
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:
enum Months { JAN=1, FEB, MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC};
#include "stdio.h"
int main()
{
enum Error {DIVIDEBYZERO =4 , NOFILE=6 , ACCESSDENIED=18, NORESOURCE=17}a, b;
a = NOFILE;
if (a == 6 )
{
printf("File does not exist.\n");
}
return 1;
}
© Nachum Danzig October 2011 - January 2017