Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C-Style Enumerations: Understanding Enumeration Declarations and Usage in C++, Study notes of C programming

The concept of c-style enumerations in c++ programming language. It covers the syntax and purpose of enumeration declarations, the object-to-integer mapping, valid enumerator declarations, and the use of enumerations as array indices. Examples are provided to illustrate the concepts.

Typology: Study notes

2021/2022

Uploaded on 09/27/2022

christina
christina 🇺🇸

4.6

(23)

404 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
12.2 C-Style Enumerations
The declaration
enum Color {RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET};
creates a new type named Color whose values are the seven colors listed between the curly
braces. Because the valid values are explicitly listed or enumerated in the declaration, this kind of
type is called an enumeration.
ENUMERATION DECLARATIONS
The declaration of an enumeration must:
1. Provide a name for the enumeration, which becomes the name of a new type
2. Explicitly list all of the values (called enumerators) of this new type
In the example above, Color is the name of the enumeration, and its enumerators are the
identifiers
RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET
When the compiler encounters such a declaration, it performs an object-to-integer mapping,
associating the integer 0 with the first identifier in this list, the integer 1 with the second, and so
on. Thus, for the preceding declaration, the compiler makes the following associations:
12.2 C-Style Enumerations 745
As another example, the declaration
enum Gender {FEMALE, MALE};
declares a new type Gender whose values are the identifiers FEMALE and MALE; the compiler will
associate the integer 0 with FEMALE and the integer 1 with MALE. Similarly, the declaration
enum HandTool {HAMMER, PLIERS, SAW, SCREWDRIVER};
constructs a new type HandTool whose values are HAMMER, PLIERS, SAW, and SCREWDRIVER, and
associates the integers 0, 1, 2, and 3 with these identifiers, respectively. By contrast, neither of
the declarations
enum Zipcodes {12531, 14405, 21724, 30081}; // ERROR!
enum LetterGrades {A, A-, B+, B, B-, C+, C, // ERROR!
C-, D+, D, D-, "FAIL" };
is a valid enumeration, because each contains items that are not valid identifiers.
C++ also allows the programmer to specify explicitly the values given to the enumerators.
For example, the declaration
enum NumberBase {BINARY = 2,
OCTAL = 8,
DECIMAL = 10,
HEX = 16, HEXADECIMAL = 16};
associates the identifiers BINARY, OCTAL, DECIMAL, HEX, and HEXADECIMAL with the values 2, 8,
10, 16, and 16, respectively. Similarly, if we wished to have the values 1, 2, . . . , 7 associated
with the seven colors given earlier (instead of 0 through 6), we could use the declaration
enum Color {RED = 1, ORANGE = 2, YELLOW = 3, GREEN = 4,
BLUE = 5, INDIGO = 6, VIOLET = 7};
or more compactly,
enum Color {RED = 1, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET};
because the integer associated with an enumerator is, by default, one more than the integer asso-
ciated with the preceding enumerator. The iostream library uses an enumeration declaration
something like
R
E
D O
R
ANG
E
Y
ELL
O
W
G
R
EE
N
B
L
U
E
I
ND
I
GO V
I
O
LET
01 23456
As another example, the declaration
enum Gender {FEMALE, MALE};
declares a new type Gender whose values are the identifiers FEMALE and MALE; the compiler will
associate the integer 0 with FEMALE and the integer 1 with MALE. Similarly, the declaration
enum HandTool {HAMMER, PLIERS, SAW, SCREWDRIVER};
constructs a new type HandTool whose values are HAMMER, PLIERS, SAW, and SCREWDRIVER,
and associates the integers 0, 1, 2, and 3 with these identifiers, respectively. By contrast, neither
of the declarations
enum Zipcodes {12531, 14405, 21724, 30081}; // ERROR!
enum LetterGrades {A, A-, B+, B, B-, C+, C, // ERROR!
C-, D+, D, D-, "FAIL" };
is a valid enumeration, because each contains items that are not valid identifiers.
C++ also allows the programmer to specify explicitly the values given to the enumerators.
For example, the declaration
pf3
pf4

Partial preview of the text

Download C-Style Enumerations: Understanding Enumeration Declarations and Usage in C++ and more Study notes C programming in PDF only on Docsity!

12.2 C-Style Enumerations

The declaration

enum Color {RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET};

creates a new type named Color whose values are the seven colors listed between the curly

braces. Because the valid values are explicitly listed or enumerated in the declaration, this kind of

type is called an enumeration.

ENUMERATION DECLARATIONS

The declaration of an enumeration must:

1. Provide a name for the enumeration, which becomes the name of a new type

2. Explicitly list all of the values (called enumerators ) of this new type

In the example above, Color is the name of the enumeration, and its enumerators are the

identifiers

RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET

When the compiler encounters such a declaration, it performs an object-to-integer mapping,

associating the integer 0 with the first identifier in this list, the integer 1 with the second, and so

on. Thus, for the preceding declaration, the compiler makes the following associations:

12.2 C-Style Enumerations 745

As another example, the declaration

enum Gender {FEMALE, MALE};

declares a new type Gender whose values are the identifiers FEMALE and MALE; the compiler will

associate the integer 0 with FEMALE and the integer 1 with MALE. Similarly, the declaration

enum HandTool {HAMMER, PLIERS, SAW, SCREWDRIVER};

constructs a new type HandTool whose values are HAMMER, PLIERS, SAW, and SCREWDRIVER, and

associates the integers 0, 1, 2, and 3 with these identifiers, respectively. By contrast, neither of

the declarations

enum Zipcodes { 12531 , 14405 , 21724 , 30081 }; // ERROR! enum LetterGrades {A, A-, B+, B, B-, C+, C, // ERROR! C-, D+, D, D-, "FAIL" };

is a valid enumeration, because each contains items that are not valid identifiers.

C++ also allows the programmer to specify explicitly the values given to the enumerators.

For example, the declaration

enum NumberBase {BINARY = 2 , OCTAL = 8 , DECIMAL = 10 , RED ORANGE YELLOW GREEN BLUE INDIGO VIOLET 0 1 2 3 4 5 6

As another example, the declaration

enum Gender {FEMALE, MALE};

declares a new type Gender whose values are the identifiers FEMALE and MALE; the compiler will

associate the integer 0 with FEMALE and the integer 1 with MALE. Similarly, the declaration

enum HandTool {HAMMER, PLIERS, SAW, SCREWDRIVER};

constructs a new type HandTool whose values are HAMMER, PLIERS, SAW, and SCREWDRIVER,

and associates the integers 0, 1, 2, and 3 with these identifiers, respectively. By contrast, neither

of the declarations

enum Zipcodes {12531, 14405, 21724, 30081}; // ERROR! enum LetterGrades {A, A-, B+, B, B-, C+, C, // ERROR! C-, D+, D, D-, "FAIL" };

is a valid enumeration, because each contains items that are not valid identifiers.

C++ also allows the programmer to specify explicitly the values given to the enumerators.

For example, the declaration

enum NumberBase {BINARY = 2, OCTAL = 8, DECIMAL = 10, HEX = 16, HEXADECIMAL = 16};

associates the identifiers BINARY, OCTAL, DECIMAL, HEX, and HEXADECIMAL with the values 2, 8,

10, 16, and 16, respectively. Because each enumerator is a power of 2, each has a 1 at a different

position in its binary representation. Such enumerators are called bit masks, and make it possible

to efficiently store a boolean value such as an iostream status attribute using only a single bit of

memory.

Similarly, if we wished to have the values 1, 2,... , 7 associated with the seven colors given

earlier (instead of 0 through 6), we could use the declaration

enum Color {RED = 1, ORANGE = 2, YELLOW = 3, GREEN = 4, BLUE = 5, INDIGO = 6, VIOLET = 7};

or more compactly,

enum Color {RED = 1, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET};

because the integer associated with an enumerator is, by default, one more than the integer

associated with the preceding enumerator. The iostream library uses an enumeration

declaration something like

enum Flag {GOOD_BIT = 1, BAD_BIT, FAIL_BIT = 4, EOF_BIT = 8};

which associates 1 with GOOD_BIT, 2 with BAD_BIT, 4 with FAIL_BIT, and 8 with EOF_BIT.^1

These examples illustrate the flexibility of C++—the integers associated with the names need

not be distinct nor must they be given in ascending order, although it is good programming style

to do so.

The general form of an enumeration declaration is as follows:

Enumeration Declaration Statement

Form:

enum TypeName { List };

where:

TypeName is an identifier naming a new type; and

List is a list of the values for the new type, separated by commas, each of which is a valid

IDENTIFIER

or an initialization expression of the form

IDENTIFIER = integer_constant

This definition builds the object colorArray as a fixed-size array with index values 0 through 6.

Because the C++ compiler treats the identifiers RED through VIOLET as the integer values 0

through 6, we can visualize colorArray as follows:

The Color enumerators can then be used with the subscript operator to access the array ele-

ments.

In the same way, an extra enumerator like NUMBER_OF_COLORS can be used to provide a vec-

tor with an initial size:

vector colorVector(NUMBER_OF_COLORS);

This defines colorVector as a varying-sized object, initially with seven elements:

  1. 0 [RED] co lorArray 0. 0 [ORANGE]
  2. 0 [YELLOW] [GREEN] [BLUE] [INDIGO] [VIOLET]
  3. 0 0. 0 0. 0 0. 0

The Color enumerators can then be used with the subscript operator to access the array

elements.

In the same way, an extra enumerator like NUMBER_OF_COLORS can be used to provide a

vector with an initial size:

vector colorVector(NUMBER_OF_COLORS);

This defines colorVector as a varying-sized object, initially with NUMBER_OF_COLORS elements:

748 Chapter 12 • Classes and Enumerations

C-STYLE ENUMERATION OPERATIONS

Many enumeration operations are predefined. For example, an enumeration object can be

assigned a value, used as a parameter, compared using the relational operators, and so on. But

other operations must be defined by the creator or user of the enumeration. For example, the pro-

gram in Figure 12-1 requires computing the wavelength corresponding to a Color value. Since

there is no predefined operation to do this, one must be provided. The function in Figure 12-

shows one way to do this. Because this Color operation is nontrivial, its definition should be

stored in the library’s implementation file Color.cpp and its prototype in the header file Color.h.

Figure 12-2 Computing Color Wavelength. // - ------ Wavelength of a Color - --------------------------------- double wavelength(Color aColor) { switch(aColor) { case RED: return 6. 5 E- 7 ; case ORANGE: return 6. 0 E- 7 ; case YELLOW: return 5. 8 E- 7 ; case GREEN: return 5. 2 E- 7 ; case BLUE: return 4. 7 E- 7 ; case INDIGO: return 4. 4 E- 7 ; case VIOLET: return 4. 1 E- 7 ; default: cerr << "\n*** wavelength: invalid color " "received!\n"; return 0. 0 ; }

  1. 0 [RED] co lorVector 0. 0 [ORANGE]
  2. 0 [YELLOW] [GREEN] [BLUE] [INDIGO] [VIOLET]
  3. 0 0. 0 0. 0 0. 0