Broad Network


Enumeration in C++

C++ Just After the Basics - Part 3

Forward: In this article, we look at the C++ enum object type.

By: Chrysanthus Date Published: 22 Aug 2012

Introduction

C++ has fundamental objects and derived objects. Derived objects are objects such as the pointer, the array and the enum object types. In this article, we look at the enum object type. Derived object types are derived from fundamental objects.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

The enum basics
enum means enumeration. The enum object type is a derived type. It is derived from integers. Let us look at a code sample containing an emun object before I explain. Just read and try the following code first:

#include <iostream>
using namespace std;

int main()
    {
        enum numbers {today, tomorrow, afterTomorow, theDayAfter};

        cout << today; cout << "\n";
        cout << tomorrow; cout << "\n";
        cout << afterTomorow; cout << "\n";
        cout << theDayAfter; cout << "\n";

        return 0;
    }

The output should display the following integers:

0
1
2
3

An enum object is a region in memory that has a range of integers. These integers are themselves, objects. Let us not worry how these integers are placed in the enum object in memory. Let us just know that an enum object is an object consisting of a range of integers. In the enum object construct, you have identifiers for the integers and not the integers themselves. So the enum object itself, has an identifier and the integers of the range that make up the object, have corresponding identifiers. In simple terms, when an enum object identifier is declared its integer identifiers for the integer objects, are implicitly assigned values (integers). In simple terms the syntax to declare an enum object and implicitly have its integer identifier objects assigned values, is:

    enum enumIdent {intIdent1, intIdent2, intIdent3, . . . }

It begins with the word, enum, then a space, then you have the identifier for the enum object itself. Then you have an optional space; then a block, obviously delimited by braces (curly brackets). Inside the block, you have identifiers for int objects. These identifiers for the int objects are not preceded by the word int, because by the enum object definition, they identify int objects. By default, the value of the first int object is zero; that of the second is 1; that of the third is 2, that of the fourth is 3, and so on. Note that the counting begins from zero and not 1. This numbering scheme can be changed (see below).

In the above code sample, the identifier for the enum object is, numbers. The identifier for the integer value, zero is, today; the identifier for the integer value, 1 is, tomorrow; the identifier for the integer value, 2 is, afterTomorow; the identifier for the integer value, 3 is, theDayAfter. It is you who decides on what name to give an identifier, whether it is the identifier for an enum object, or identifier for an int object inside the enum block.

There are four cout lines above for the four integers in the sample code. A cout statement uses the identifier of an integer in the enum set to print the integer. Each line for an output actually has two cout statements. Remember, a statement ends with a semicolon. Do not confuse between a line and a statement.

The second statement in each line prints an un-displayed character, ‘n’. This is a character, even though it is made up of two symbols, and n. The n character is not displayed, but it causes what is to be displayed next in the command prompt window, to be displayed on the next line. That is why you have the numbers, 0, 1, 2, and 3, displayed in separate lines.

Constant
The identifiers of the integers in the enum set are constants. This means that you cannot change their values after the enum object is created. So the following code will not compile because an attempt is made to change the value of one of the integer constants:

#include <iostream>
using namespace std;

int main()
    {
        enum numbers {today, tomorrow, afterTomorow, theDayAfter};

        tomorrow = 6;

        return 0;
    }

As said above, the identifiers in the enumerator-list are declared as constants, for constant values.

Non-Zero Start Integer
In the above example, the value of the first integer is zero. You can have some other number as the start integer. To achieve this, you will need an assignment expression for the first constant integer in the enumeration list. The following code illustrates this. Read and try it.

#include <iostream>
using namespace std;

int main()
    {
        enum numbers {today=-1, tomorrow, afterTomorow, theDayAfter};

        cout << today; cout << "\n";
        cout << tomorrow; cout << "\n";
        cout << afterTomorow; cout << "\n";
        cout << theDayAfter; cout << "\n";

        return 0;
    }

The output should display the following integers:

-1
0
1
2


There are four identifiers in the enumeration list. We forced the first one to be –1. So the second identifier has the value, 0, the third has the value 1 and the fourth has the value 2.

Note: In the enumeration list the difference between the values of the identifiers is 1, everything being equal.

Modifying the Range
In the enumeration list, the difference between the values of the identifiers is 1, everything being equal. The range is determined by the value of the first identifier. If no value is assigned to any of the identifiers in the list, then the range begins from zero, adding 1 to the different identifier values until the last identifier in the list.

If a value (integer) is assigned to the first identifier, then the range begins from that number, adding 1 until the last identifier; that is if no other identifier is assigned a value. That is how the enum construct works.

Now, to modify the range, you can force an identifier (other than the first) in the list to have a value that is out of the expected range. The identifiers after this forced value will have values increasing by 1 but continuing from that of the forced value. To achieve this, you just assign the out of expected range value to an identifier in the list. You can force the values for more than one identifier in the list. The following code illustrates this:

#include <iostream>
using namespace std;

int main()
    {
        enum numbers {today=10, tomorrow =56, afterTomorow=22, theDayAfter, dayEvenAfter};

        cout << today; cout << "\n";
        cout << tomorrow; cout << "\n";
        cout << afterTomorow; cout << "\n";
        cout << theDayAfter; cout << "\n";
        cout << dayEvenAfter; cout << "\n";

        return 0;
    }

The output is:

10
56
22
23
24

We made the start value, 10. The next value was supposed to be 11, but we forced it to 56. The value after was then supposed to be 57 but we forced it to 22. Since we did not force any value for the following identifier, it was 23 according to the enum rule (adding 1). The rest of the values were not forced so they just had increment of 1 from the previous value.

Note: enum values accept only integers.  

That is what I have prepared for enumeration in C++. I hope you appreciated it. We continue in the next part of the series.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message