In C and C++, enum types can be used to set up collections of named integer constants. (The keyword enum is short for ``enumerated''.)
The traditional C way of doing this was something like this:
#define SPRING 0
#define SUMMER 1
#define FALL 2
#define WINTER 3
An alternate approach using enum would be
enum { SPRING, SUMMER, FALL, WINTER };
enum declarations
There are two kinds of enum type declarations. One kind creates a named type, as in
enum MyEnumType { ALPHA, BETA, GAMMA };
If you give an enum type a name, you can use that type for variables, function arguments and return values, and so on:
enum MyEnumType x; /* legal in both C and C++ */
MyEnumType y; // legal only in C++
The other kind creates an unnamed type. This is used when you want names for constants but don't plan to use the type to declare variables, function arguments, etc. For example, you can write
enum { HOMER, MARGE, BART, LISA, MAGGIE };
Values of enum constants
If you don't specify values for enum constants, the values start at zero and increase by one with each move down the list. For example, given
enum MyEnumType { ALPHA, BETA, GAMMA };
ALPHA has a value of 0, BETA has a value of 1, and GAMMA has a value of 2.
If you want, you may provide explicit values for enum constants, as in
enum FooSize { SMALL = 10, MEDIUM = 100, LARGE = 1000 };
C++ enum type conversion rules
These rules apply to C++ only. The rules for C are different.
There is an implicit conversion from any enum type to int. Suppose this type exists:
enum MyEnumType { ALPHA, BETA, GAMMA };
Then the following lines are legal:
int i = BETA; // give i a value of 1
int j = 3 + GAMMA; // give j a value of 5
On the other hand, there is not an implicit conversion from int to an enum type:
MyEnumType x = 2; // should NOT be allowed by compiler
MyEnumType y = 123; // should NOT be allowed by compiler
The C++ Modulus Operator
Take a simple arithmetic problem: what's left over when you divide 11 by 3? The answer is easy to compute: divide 11 by 3 and take the remainder: 2. But how would you compute this in a programming language like C or C++? It's not hard to come up with a formula, but the language provides a built-in mechanism, the modulus operator ('%'), that computes the remainer that results from performing integer division.
The modulus operator is useful in a variety of circumstances. It is commonly used to take a randomly generated number and reduce that number to a random number on a smaller range, and it can also quickly tell you if one number is a factor of another.
If you wanted to know if a number was odd or even, you could use modulus to quickly tell you by asking for the remainder of the number when divided by 2.
#include
using namespace std;
int main()
{
int num;
cin >> num;
// num % 2 computes the remainder when num is divided by 2
if ( num % 2 == 0 )
{
cout << num << " is even ";
}
return 0;
}
The key line is the one that performs the modulus operation: "num % 2 == 0". A number is even if and only if it is divisible by two, and a number is divisible by another only if there is no remainder.