int, char, double, float, unsigned, and more.
Values are stored uniquely in memory, but bits can be interpreted in various ways.
Example   0100 0001
The system views the bits in the context in which they occur in the code.
Examples to trace
1. letter = 'A'; y = letter++; y: ? letter: ?
2. letter = 'A'; y = ++letter; y: ? letter: ?
Example
int x; bool is_odd; cin >> x; if (x % 2) is_odd = true; // better? is_odd = x % 2; else is_odd = false; // display 0 if false, 1 if true cout << is_odd << endl; |
Example
enum Colors {red, blue, yellow, pink, aqua}; // new type
int main()
{
Colors shirt, car = red; // declare variables
// Assume initialization of shirt
if(shirt < car) cout << shirt << " is less than " << car << endl;
else if(shirt == car) cout << shirt << " is equal to " << car << endl;
else cout << shirt << " is greater than " << car << endl;
}
|
output:
In our example, red is 0, blue is 1, yellow is 2, etc.
_______________________
Notes:
1) enummerated types are ordinal types, thus are ordered, 0, 1, 2,...
2) The programmer sets the valid values which must be
a) identifiers
b) unique
3) Being unique means that values of enumerated types cannot be characters,
  strings, numeric constants, or any valid value for any other type.
4) Types are a template only; they do NOT have storage allocated to them.
5) Above, variables shirt and car have storage but type Colors does not.
_______________________
Examples
We can force enumeration to begin with an integer other than zero. Here we begin with the value 1 to correspond with the traditional numbering of months. In our examples, jan is 1, and it is implied that feb is 2, march is 3, etc.
#include <iostream>
using namespace std;
enum Month {jan = 1, feb, march, april, may, june}; // define new type
void process(Month);
int main()
{
Month warm, cold = jan; // allocate memory
// ASSUME INITIALIZATION OF warm
switch(warm) {
case jan: process(jan); break;
case feb: process(feb); break;
case march: process(march); break;
case april: process(april); break;
case may: process(may); break;
case june: process(june); break;
default: cout << warm << " is an illegal value.\n";
}
}
// This is a stub to be sure correct argument got to function
void process(Month which_month)
{
cout << "In process() which_month has the value " << which_month << ".\n";
}
|
We cannot do the following:
enum Vowels {'a', 'e', 'i', 'o', 'u'};
Vowels letter;
letter = 'i'; // What type is 'i', Vowel or char?
|
____________________________
Example
Enumerated types can be used in expressions, cout statements, and many place
integers can be used. Unfortunately, C++ does not allow us to use cin
with enumerated types.
enum Vowels {a, e, i, o, u, y};
Vowels letter;
// ASSUME THAT letter HAS BEEN INITIALIZED
switch (letter) {
case a:
case e:
case i:
case o:
case y:
process_aeioy(letter); break;
case u:
process_u(); break;
default:
cout << "letter must be a e i o u y\n";
};
|
____________________________
Now let's think about type bool again. Language C does not have type bool. If you were programming in C rather than C++ and wanted to implement type bool, how would you do it using enumerated types? (Enumerated types are the same in both languages.)
If you wanted to implement a bool type of your own in C++, you could not use the identifiers bool, true, or false. (Why?) You would have to use something else, such as my_bool, my_true, and my_false. For practice, implement type my_bool, declare some variables, assign values, etc.