enum - A Simple Example of a User Defined Type
David B. Sher
Mat/Sta/CMP Department
Nassau Community College

A major step forward in program is to design your own types to represent data types. The enumerated type is the simplest form of programmer defined type. An enumerated type is a type that can only take on a fixed set of values. For example: a Boolean type has only two values, true and false. Another example of an enumerated type would be card suits for a playing card program. The values for a suit type would be: spades, hearts, diamonds and clubs.

C++ defines an enumerated type with the enum keyword. The name of the type follows the keyword. Following the name is a list of values for the type surrounded by {}'s. This is followed by optional variables of this type and a required ';'. An example of a definition of an enum is below:

// this defines a type for a suit for a deck of cards
enum suit { spades, hearts, diamonds, clubs } /* no variables */;

Once you define an enumerated type you can use it just like a built in type such as float. You can define variables of that type and also pointers and references for that type. You can also define a function that returns that type or has parameters of that type. Generally you can define anything you like for that type. Examples of functions that use the suit type are below:

suit readSuit(istream& input) // reads a suit value from an istream
char suitString[256]; // will hold suit word
input >> suitString; // reads suit from input
switch(suitString[0]) // only use first letter to decide suit
case 's': return spades; // we assume any word beginning with s is spades
case 'h': return hearts; // we assume any word beginning with h is hearts
case 'd': return diamonds; // we assume any word beginning with d is diamonds
case 'c': return clubs; // we assume any word beginning with c is clubs
default: assert(NULL=="Unknown value for suit from istream");// otherwise error
} // end of switch
return spades; // default return statement for compiler
} // end of readSuit

Enumerated types are implemented in C++ as integers. Data of any type needs to be stored in memory and represented in binary. The values of an enumerated type are represented as integers on the computer. They are not integers, but they are represented that way. In the suit type spades could be represented as 0, hearts as 1, diamonds as 2, and clubs as 3. However there is no requirement that these numbers be used. They could be -3,-2,-1 and 0 respectively. You can use ++ on an enumerated type variable to move from one value to the following one.

You may want to specify the integer representations of the values of an enumerated type. For example if you want to define an alternative Boolean type you can make sure that true has a representation of 1 and false has a representation of 0. Or you can have a type that represents colors use the HTML representation of those colors. The way you do this is shown below:

enum Boolean { true=1, false=0 }; // alternative boolean type

// represents colors for html page
enum color

}; // end of color

Whenever you have a set of special codes to represent status (for example the status of a file stream) you use an enumerated type. Other user defined types - structs and classes - use similar formats.