Here is a fully operational program that shows ten different types of operations with enums. Details will be in the code, references will be at the bottom of the post.
First, it is very important you understand what enums are. For a reference on this, take a look at Enumerated Types - enums. In the code below I use mathematical operations, bitwise operators and use an enum value to initialize an array to a default size.
#include <iostream>
using namespace std;
enum EnumBits
{
ONE = 1,
TWO = 2,
FOUR = 4,
EIGHT = 8
};
enum Randoms
{
BIG_COUNT = 20,
INTCOUNT = 3
};
int main(void)
{
// Basic Mathimatical operations
cout << (ONE + TWO) << endl; // Value will be 3.
cout << (FOUR - TWO) << endl; // Value will be 2.
cout << (TWO * EIGHT) << endl; // Value will be 16.
cout << (EIGHT / TWO) << endl; // Value will be 4.
// Some bitwise operations
cout << (ONE | TWO) << endl; // Value will be 3.
cout << (TWO & FOUR) << endl; // Value will be 0.
cout << (TWO ^ EIGHT) << endl; // Value will be 10.
cout << (EIGHT << 1) << endl; // Value will be 16.
cout << (EIGHT >> 1) << endl; // Value will be 4.
// Initialize an array based upon an enum value
int intArray[INTCOUNT];
// Have a value initialized be initialized to a static value plus
// a value to be determined by an enum value.
int someVal = 5 + BIG_COUNT;
return 0;
}
The code sample done above can be done in another way, which is where you overload the operator|, etc for EnumBits. This is a commonly used technique, for additional references please see How to use enums as flags in C++?.
For a reference on bitwise operations, take a look at Bitwise Operators in C and C++: A Tutorial
Using C++ 11 you can use enums in additional ways, such as strongly typed enums.