How to use Enumerated constants in C++

By: Emiley J Emailed: 1645 times Printed: 2119 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

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 COLOR: RED, BLUE, GREEN, WHITE, and BLACK.

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:


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 0, BLUE a symbolic constant with the value 1, GREEN 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 100; BLUE, the value 101; GREEN, the value 500; WHITE, 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, RED, BLUE, GREEN, WHITE, or BLACK, or else 100, 101, 500, 501, 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 };
6:       Days DayOff;
7:       int x;
9:       cout << "What day would you like off (0-6)? ";
10:      cin  >> x;
11:      DayOff = Days(x);
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.

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 Emiley J
Password must include both numeric and alphabetic characters - Magento
What is Hadoop?
Returning multiple values from a web service
Tomcat and httpd configured in port 8080 and 80
Java Webservices using Netbeans and Tomcat
Java WebService connected to Database
How to Deploy a Java Web Service
Call a webservice in Java
Java WebService - Create your first web service in Java
package javax.jws does not exist
Getting Started with Android
HTML5 Location - getCurrentPosition() in HTML5
HTML5 Canvas - Using Canvas in HTML5
HTML5 - Introduction
HTML5 Video - Handling video in HTML5

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++ )
Calculating total based on the given quantity and price in C++
Using cout.width() in C++
C++ Recursion function explained using Fibonacci series
Using switch Statements in C++
Difference between Procedural, Structured, and Object-Oriented Programming
Using peek() and putback() with cin in C++
Using cout.fill() in C++
Sum of 1 to N in C++
Where to Put Class Declarations and Method Definitions in C++
while (1) Loops in C++
Dot (.) vs Arrow (->) to access data members in C++
What Is a Reference in C++?
C ++ Reference Parameters Versus Java Reference Parameters
Converting C++ Abstract Classes into Java Interfaces
strcpy() and strncpy() sample program in C++
Most Emailed Articles (in C++)
Sorting an array of Strings in C++
Public versus Private members in C++
The indirection operator (*) - dereference operator.
const Pointers in C++
Returning values from a function in C++
while (1) Loops in C++
Using Comments in a C++ Program
Demonstrating global and local variables in C++
Specifying default values to function parameters in C++
continue and break statements 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++
Calculating total based on the given quantity and price in C++
while Loops in C++
Manipulating Data by Using Pointers