Programming & Scripting Tutorials

C++: Enumerations





Enumerations allow programmers to define there own datatypes which usually take on a small number of values.
For example if we were writing a card game we could define all of the suits manually like this:

int Clubs = 0;
int Spades = 1;
int Hearts = 2;
int Diamonds = 3;

Or we could do it more properly using enumerations, like so:

enum Suit{Clubs,
Spades,
Hearts,
Diamonds};

By default the enumeration starts at 0, so Clubs would be 0, spades would be 1 etc.. Although this usually suits our needs, sometimes we need to start on a different number, we do this very simply like so:


enum Suit{Clubs = 5,
Spades,
Hearts,
Diamonds};


The above example would make clubs 5, spades 6 etc..

Enumerations are often useful, and you will now find that because you know them you will use them in a lot of programs.


This C++ tutorial was written by


Back to C++

Advertisement: