Enumerations:
The C/C++ enumeration syntax exists to support the use of human readable character names to support a specific list of available values for the specified variable.
This is enabled in C and C++ as a set of named integer constants.
This can be expressed as #define preprocessor directives or as a C or C++ enumerated type "enum".
Both C and C++ use the same enumeration syntax.
We also give an example of a C++ enumeration class which extends the capability of the C enumeration.
C / C++ Enumeration Syntax and Example:
Basic C/C++ enumeration declaration and use:
File:
enumExample.cpp
08 | monday, tuesday, wednesday, thursday, friday, saturday, sunday |
13 | if (day == saturday || day == sunday) |
14 | cout << "Day is a weekend day" << endl; |
15 | else if (day == wednesday) |
16 | cout << "Day is hump day - middle of the work week" << endl; |
Result:
Day is hump day - middle of the work week
Enumerations are represented as incrementing integers beginning with the value 0.
One can specify the integer representation of the enumeration by specifying the integer from which the index begins incrementing.
More than one enumeration can be represented by the same integer.
File:
enumExample2.cpp
20 | cout << "Day is a weekend day" << endl; |
21 | else if (day == wednesday) |
22 | cout << "Day is hump day - middle of the work week" << endl; |
Result:
Day is a weekend day
File:
enumExample3.cpp
17 | TEENS teen = seventeen; |
20 | cout << seventeen << endl; |
Result:
14
Obviously if we started the enumeration at 13 instead of 10 we would have had alignment with the integer in our results.
C++ Enumeration Class:
One can create and enumeration class in C++ to extend the capabilities of enumerations.
File:
CDay.hpp
25 | explicit Day( const std::string& ss); |
28 | Day& operator = ( const Day& cc); |
29 | Day& operator = ( const std::string& ss); |
30 | Day& operator = (Enum ee); |
33 | bool operator< ( const Day& cc) const ; |
34 | bool operator< (Enum ee) const ; |
35 | bool operator<= ( const Day& cc) const ; |
36 | bool operator<= (Enum ee) const ; |
37 | bool operator> ( const Day& cc) const ; |
38 | bool operator> (Enum ee) const ; |
39 | bool operator>= ( const Day& cc) const ; |
40 | bool operator>= (Enum ee) const ; |
41 | bool operator== ( const Day& cc) const ; |
42 | bool operator== ( const std::string& ss) const ; |
43 | bool operator== ( const Enum ee) const ; |
44 | bool operator!= ( const Day& cc) const ; |
45 | bool operator!= ( const std::string& ss) const ; |
46 | bool operator!= ( const Enum ee) const ; |
49 | inline std::string getString ( void ) const ; |
50 | inline Enum getEnum ( void ) const ; |
51 | inline int getValue ( void ) const ; |
55 | static Enum fromString(std::string ss); |
56 | static std::string toString(Enum ee); |
57 | static int toValue(Enum ee); |
65 | inline std::ostream& operator<< (std::ostream& _os, const Day& _e) |
67 | _os << _e.getString(); |
71 | inline std::string Day::getString( void ) const |
76 | Day::Enum Day::getEnum( void ) const |
81 | int Day::getValue( void ) const |
File:
CDay.cpp
012 | m_string(toString(_e)), |
016 | Day::Day( const std::string& _s) : |
017 | m_enum(fromString(_s)), |
019 | m_value(toValue(m_enum)) |
024 | Day& Day::operator= ( const Day& _c) |
026 | m_string = _c.m_string; |
028 | m_value = _c.m_value; |
032 | Day& Day::operator= ( const std::string& _s) |
035 | m_enum = fromString(_s); |
036 | m_value = toValue(m_enum); |
040 | Day& Day::operator= (Enum _e) |
043 | m_string = toString(_e); |
044 | m_value = toValue(_e); |
048 | bool Day::operator< ( const Day& _c) const |
050 | return (m_value < _c.m_value); |
053 | bool Day::operator< (Enum _e) const |
055 | return (m_value < toValue(_e)); |
058 | bool Day::operator<= ( const Day& _c) const |
060 | return (m_value <= _c.m_value); |
063 | bool Day::operator<= (Enum _e) const |
065 | return (m_value <= toValue(_e)); |
068 | bool Day::operator> ( const Day& _c) const |
070 | return (m_value > _c.m_value); |
073 | bool Day::operator> (Enum _e) const |
075 | return (m_value > toValue(_e)); |
078 | bool Day::operator>= ( const Day& _c) const |
080 | return (m_value >= _c.m_value); |
083 | bool Day::operator>= (Enum _e) const |
085 | return (m_value >= toValue(_e)); |
088 | bool Day::operator== ( const Day& _c) const |
090 | return (m_enum == _c.m_enum); |
093 | bool Day::operator== ( const std::string& _s) const |
095 | return (m_string == _s); |
098 | bool Day::operator== ( const Enum _e) const |
100 | return (m_enum == _e); |
103 | bool Day::operator!= ( const Day& _c) const |
105 | return (m_enum != _c.m_enum); |
108 | bool Day::operator!= ( const std::string& _s) const |
110 | return (m_string != _s); |
113 | bool Day::operator!= ( const Enum _e) const |
115 | return (m_enum != _e); |
118 | Day::Enum Day::fromString(std::string _s) |
121 | transform(_s.begin(), _s.end(), _s.begin(), toupper ); |
122 | if (_s == "SUNDAY" ) return sunday; |
123 | else if (_s == "MONDAY" ) return monday; |
124 | else if (_s == "TUESDAY" ) return tuesday; |
125 | else if (_s == "WEDNESDAY" ) return wednesday; |
126 | else if (_s == "THURSDAY" ) return thursday; |
127 | else if (_s == "FRIDAY" ) return friday; |
128 | else if (_s == "SATURDAY" ) return saturday; |
130 | throw std::range_error( "Not a valid Day value: " + _s); |
134 | std::string Day::toString(Day::Enum _e) |
138 | case sunday: { return "SUNDAY" ; } |
139 | case monday: { return "MONDAY" ; } |
140 | case tuesday: { return "TUESDAY" ; } |
141 | case wednesday: { return "WEDNESDAY" ; } |
142 | case thursday: { return "THURSDAY" ; } |
143 | case friday: { return "FRIDAY" ; } |
144 | case saturday: { return "SATURDAY" ; } |
149 | int Day::toValue(Day::Enum _e) |
153 | case sunday: { return 0; } |
154 | case monday: { return 2; } |
155 | case tuesday: { return 3; } |
156 | case wednesday: { return 4; } |
157 | case thursday: { return 5; } |
158 | case friday: { return 6; } |
159 | case saturday: { return 7; } |
File:
dayProg.cpp
11 | if (day == Day::saturday || day == Day::sunday) |
12 | cout << "Day is a weekend day" << endl; |
Compile:
g++ CDay.cpp dayProg.cpp
Results:
Day is a weekend day
Other Methodologies:
Associated string array:
File:
enumExample5.cpp
05 | monday, tuesday, wednesday, thursday, friday, saturday, sunday |
08 | const char *day_str[]={ "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" , "Saturday" , "Sunday" }; |
16 | if (day == saturday || day == sunday) |
17 | cout << day_str[day] << endl; |
Compile:
g++ enumExample5.cpp
Run:
./a.out
Result:
Saturday
C define macros:
File:
enumExample6.cpp
19 | if (day == SATURDAY || day == SUNDAY) |
20 | cout << "Day is a weekend day" << endl; |
Compile:
g++ enumExample6.cpp
Run:
./a.out
Result:
Day is a weekend day
Other methodologies could include STL Map or Boost smart_enum constructs.

Books: