RSS

How to use Enumerated constants in C++


Enumerated constants enable you to create new types and then to define variables of those types whose values are restricted to a set of possible values. For example, you can declare COLOR to be an enumeration, and you can define that there are five values for COLORREDBLUEGREENWHITE, andBLACK.
The syntax for enumerated constants is to write the keyword enum, followed by the type name, an open brace, each of the legal values separated by a comma, and finally a closing brace and a semicolon. Here's an example:
enum COLOR { RED, BLUE, GREEN, WHITE, BLACK };
This statement performs two tasks:
1. It makes COLOR the name of an enumeration, that is, a new type.

2. It makes RED a symbolic constant with the value 0BLUE a symbolic constant with the value 1GREEN a symbolic constant with the value 2, and so forth.
Every enumerated constant has an integer value. If you don't specify otherwise, the first constant will have the value 0, and the rest will count up from there. Any one of the constants can be initialized with a particular value, however, and those that are not initialized will count upward from the ones before them. Thus, if you write
enum Color { RED=100, BLUE, GREEN=500, WHITE, BLACK=700 };
then RED will have the value 100BLUE, the value 101GREEN, the value 500WHITE, the value 501; and BLACK, the value 700.
You can define variables of type COLOR, but they can be assigned only one of the enumerated values (in this case, REDBLUEGREENWHITE, or BLACK, or else 100101500501, or 700). You can assign any color value to your COLOR variable. In fact, you can assign any integer value, even if it is not a legal color, although a good compiler will issue a warning if you do. It is important to realize that enumerator variables actually are of type unsigned int, and that the enumerated constants equate to integer variables. It is, however, very convenient to be able to name these values when working with colors, days of the week, or similar sets of values. Listing below presents a program that uses an enumerated type.
A demonstration of enumerated constants.
1:  #include <iostream.h>
2:  int main()
3:  {
4:       enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,                     Â_Saturday };
5:
6:       Days DayOff;
7:       int x;
8:
9:       cout << "What day would you like off (0-6)? ";
10:      cin  >> x;
11:      DayOff = Days(x);
12:
13:      if (DayOff == Sunday || DayOff == Saturday)
14:            cout << "\nYou're already off on weekends!\n";
15:      else
16:            cout << "\nOkay, I'll put in the vacation day.\n";
17:       return 0;
18: }
Output: What day would you like off (0-6)?  1

Okay, I'll put in the vacation day.

What day would you like off (0-6)?  0

You're already off on weekends!
Analysis: On line 4, the enumerated constant DAYS is defined, with seven values counting upward from 0. The user is prompted for a day on line 9. The chosen value, a number between 0 and 6, is compared on line 13 to the enumerated values for Sunday and Saturday, and action is taken accordingly.

The if statement will be covered in more detail on Day 4, "Expressions and Statements."
You cannot type the word "Sunday" when prompted for a day; the program does not know how to translate the characters in Sunday into one of the enumerated values.

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

Demonstration of Prefix and Postfix operators in C++


Both the increment operator (++) and the decrement operator(--) come in two varieties: prefix and postfix. The prefix variety is written before the variable name (++myAge); the postfix variety is written after (myAge++).
In a simple statement, it doesn't much matter which you use, but in a complex statement, when you are incrementing (or decrementing) a variable and then assigning the result to another variable, it matters very much. The prefix operator is evaluated before the assignment, the postfix is evaluated after.
The semantics of prefix is this: Increment the value and then fetch it. The semantics of postfix is different: Fetch the value and then increment the original.
This can be confusing at first, but if x is an integer whose value is 5 and you write
int a = ++x;
you have told the compiler to increment x (making it 6) and then fetch that value and assign it to a. Thus, a is now 6 and x is now 6.
If, after doing this, you write
int b = x++;
you have now told the compiler to fetch the value in x (6) and assign it to b, and then go back and increment x. Thus, b is now 6, but x is now 7. Listing below shows the use and implications of both types.
A demonstration of prefix and postfix operators.
1:  // Listing 4.3 - demonstrates use of
2:  // prefix and postfix increment and
3:  // decrement operators
4:  #include <iostream.h>
5:  int main()
6:  {
7:      int myAge = 39;      // initialize two integers
8:      int yourAge = 39;
9:      cout << "I am: " << myAge << " years old.\n";
10:     cout << "You are: " << yourAge << " years old\n";
11:     myAge++;         // postfix increment
12:     ++yourAge;       // prefix increment
13:     cout << "One year passes...\n";
14:     cout << "I am: " << myAge << " years old.\n";
15:     cout << "You are: " << yourAge << " years old\n";
16:     cout << "Another year passes\n";
17:     cout << "I am: " << myAge++ << " years old.\n";
18:     cout << "You are: " << ++yourAge << " years old\n";
19:     cout << "Let's print it again.\n";
20:     cout << "I am: " << myAge << " years old.\n";
21:     cout << "You are: " << yourAge << " years old\n";
22:       return 0;
23: }
Output: I am      39 years old
You are   39 years old
One year passes
I am      40 years old
You are   40 years old
Another year passes
I am      40 years old
You are   41 years old
Let's print it again
I am      41 years old
You are   41 years old
Analysis: On lines 7 and 8, two integer variables are declared, and each is initialized with the value 39. Their values are printed on lines 9 and 10.
On line 11, myAge is incremented using the postfix increment operator, and on line 12, yourAge is incremented using the prefix increment operator. The results are printed on lines 14 and 15, and they are identical (both 40).
On line 17, myAge is incremented as part of the printing statement, using the postfix increment operator. Because it is postfix, the increment happens after the print, and so the value 40 is printed again. In contrast, on line 18, yourAge is incremented using the prefix increment operator. Thus, it is incremented before being printed, and the value displays as 41.
Finally, on lines 20 and 21, the values are printed again. Because the increment statement has completed, the value in myAge is now 41, as is the value in yourAge.

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS