I've read through the section in my book about enumerations multiple times, and watched several videos. For some reason I just can't grasp the idea of enumerations. Can someone give me a detailed explanation of a enumeration, and a example or two of how they are used? You might have to slightly dumb it down, I'm fairly new to programming. Thanks for any help in advance.
Once upon a time, people used to define symbolic constants via macros, like:
1 2 3 4
#define ONE 1
#define TWO 2
#define THREE 3
#define FOUR 5 // oops, mistake
Then they thought, how can we make the process easier? And so enum was invented.
1 2 3 4 5 6 7 8
enum {
ONE = 1, // because it would normally start from 0, reset count to 1
TWO, // 2 by default
THREE, // 3 by default
SIXTY = 60,
SIXTYONE, // 61 by default
SIXTYTWO // 62 by default
};
Now you can just use ONE, TWO, on their own.
You can also give your enum collection a label. In C++ it would work like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
enum Days {
Monday, // 0 by default
Tuesday, // 1 by default ...
Wednesday,
Thursday,
Friday,
Saturday,
Sunday // 6 by default
};
void dayFunction(Days d) // this function expects a day from Monday to Sunday
{
}
int main()
{
dayFunction(Wednesday);
}