Enumerations in C++

By: Stanley B. Emailed: 1697 times Printed: 2193 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Often we need to define a set of alternative values for some attribute. A file, for example, might be open in one of three states: input, output, and append. One way to keep track of these state values might be to associate a unique constant number with each. We might write the following:

const int input = 0;
const int output = 1;
const int append = 2;

Although this approach works, it has a significant weakness: There is no indication that these values are related in any way. Enumerations provide an alternative method of not only defining but also grouping sets of integral constants.

Defining and Initializing Enumerations

An enumeration is defined using the enum keyword, followed by an optional enumeration name, and a comma-separated list of enumerators enclosed in braces.

// input is 0, output is 1, and append is 2
enum open_modes {input, output, append};

By default, the first enumerator is assigned the value zero. Each subsequent enumerator is assigned a value one greater than the value of the enumerator that immediately precedes it.

Enumerators Are const Values

We may supply an initial value for one or more enumerators. The value used to initialize an enumerator must be a constant expression. A constant expression is an expression of integral type that the compiler can evaluate at compile time. An integral literal constant is a constant expression, as is a const object that is itself initialized from a constant expression.

For example, we might define the following enumeration:

// shape is 1, sphere is 2, cylinder is 3, polygon is 4
enum Forms {shape = 1, sphere, cylinder, polygon};

In the enum Forms we explicitly assigned shape the value 1. The other enumerators are implicitly initialized: sphere is initialized to 2, cylinder to 3, and polygon to 4.

An enumerator value need not be unique.

// point2d is 2, point2w is 3, point3d is 3, point3w is 4
enum Points { point2d = 2, point2w,
point3d = 3, point3w };

In this example, the enumerator point2d is explicitly initialized to 2. The next enumerator, point2w, is initialized by default, meaning that its value is one more than the value of the previous enumerator. Thus, point2w is initialized to 3. The enumerator point3d is explicitly initialized to 3, and point3w, again is initialized by default, in this case to 4.

It is not possible to change the value of an enumerator. As a consequence an enumerator is itself a constant expression and so can be used where a constant expression is required.

Each enum Defines a Unique Type

Each enum defines a new type. As with any type, we can define and initialize objects of type Points and can use those objects in various ways. An object of enumeration type may be initialized or assigned only by one of its enumerators or by another object of the same enumeration type:

Points pt3d = point3d; // ok: point3d is a Points enumerator
Points pt2w = 3; // error: pt2w initialized with int
pt2w = polygon; // error: polygon is not a Points enumerator
pt2w = pt3d; // ok: both are objects of Points enum type

Note that it is illegal to assign the value 3 to a Points object even though 3 is a value associated with one of the Points enumerators.

C++ Home | All C++ Tutorials | Latest C++ Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Bookmark and Share


Be the first one to add a comment

Your name (required):

Your email(required, will not be shown to the public):

Your sites URL (optional):

Your comments:

More Tutorials by Stanley B.
Enumerations in C++
typedef in C++
Declarations and Definitions in C++
Uninitialized Variables Cause Run-Time Problems in C++
C++ keywords
Lvalues and Rvalues in C++
Multi-Line Literals in C++
Concatenated String Literals in C++
Character String Literals in C++
Escape Sequences for Nonprintable Characters in C++
Using the Built-in Arithmetic Types in C++
if in C++
Programming errors a compiler will detect in C++
for loop in C++
while loop in C++

More Tutorials in C++
Two-Dimensional Array Manipulation in C++
Calculate average using Two-Dimensional Array in C++
Compute the square root of the sum of the squares of an array in C++
Matrix using nested for loops in C++
Sorting an array of Strings in C++
Calculating total based on the given quantity and price in C++
Compiling and Linking Multiple Source Files in C++
Enumerations in C++
Program to add two numbers in C++
Comments in C++
while loop in C++
for loop in C++
Programming errors a compiler will detect in C++
if in C++
Using the Built-in Arithmetic Types in C++

More Latest News
Most Viewed Articles (in C++ )
Dot (.) vs Arrow (->) to access data members in C++
Using Comments in a C++ Program
Reverse a String in C++
Difference between Procedural, Structured, and Object-Oriented Programming
Demonstration of Prefix and Postfix operators in C++
Constructors and Destructors in C++
while Loops in C++
while (1) Loops in C++
Using switch Statements in C++
The indirection operator (*) - dereference operator.
Stray or Dangling Pointers in C++
Operator Precedence in C++
atoi(), itoa() in C++
strcpy() and strncpy() sample program in C++
assert() example program in C++
Most Emailed Articles (in C++)
Using cout in C++
Demonstration of Prefix and Postfix operators in C++
qsort() sample program in C++
Using #define, The Preprocessor and the Compiler in C++
Escape Sequences for Nonprintable Characters in C++
A Brief History of C++
C++ and Object-Oriented Programming
Use of Conditional (Ternary) Operator in C++
Returning values from a function in C++
Constructors and Destructors in C++
Interface Versus Implementation in C++
The Stack and the Free Store in C++
this Pointer in C++
How to handle Exceptions in C++
Using cout.width() in C++