C++ Enumeration
Enumeration
 As we’ve seen, structures can be looked at as a way to provide user-defined
data types. A different
 approach to defining your own data type is the enumeration. This feature of
C++ is less
 crucial than structures. You can write perfectly good object-oriented
programs in C++ without
 knowing anything about enumerations. However, they are very much in the
spirit of C++, in
 that, by allowing you to define your own data types, they can simplify and
clarify your programming
Enumeration
 Enum in C++ is a data type that contains fixed set of
constants.
 It can be used for days of the week (SUNDAY, MONDAY,
TUESDAY, WEDNESDAY, THURSDAY, FRIDAY and SATURDAY) ,
directions (NORTH, SOUTH, EAST and WEST) etc. The C++
enum constants are static and final implicitly.
 C++ Enums can be thought of as classes that have fixed
set of constants.
Points to remember for C++ Enum
 enum improves type safety
 enum can be easily used in switch
 enum can be traversed
 enum can have fields, constructors and methods
 enum may implement many interfaces but cannot extend
any class because it internally extends Enum class
Days of the Week Enumeration
C++ Enumeration Example
1. #include <iostream>
2. using namespace std;
3. enum week { Monday, Tuesday, Wednesday, Thursday, Friday,
Saturday, Sunday };
C++ Enumeration Example
4. int main()
5. {
6. week day;
7. day = Friday;
8. cout << "Day: " << day+1<<endl;
9. return 0;
10. }
Output:
Day: 5
C++ Enumeration Example 2
#include <iostream>
using namespace std;
//specify enum type
enum days_of_week { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
int main()
{
days_of_week day1, day2; //define variables
//of type days_of_weekint main()
{
days_of_week day1, day2; //define variables
//of type days_of_week
C++ Enumeration Example 2
day1 = Mon; //give values to
day2 = Thu; //variables
int diff = day2 - day1; //can do integer arithmetic
cout << “Days between = “ << diff << endl;
if(day1 < day2) //can do comparisons
cout << “day1 comes before day2n”;
return 0;
}
Explanation
 An enumeration is a list of all possible values. This is
unlike the specification of an int, for example, which is
given in terms of a range of values. In an enum you must
give a specific name to every possible value.
Difference between an int and an enum.

Enum

  • 1.
  • 2.
    Enumeration  As we’veseen, structures can be looked at as a way to provide user-defined data types. A different  approach to defining your own data type is the enumeration. This feature of C++ is less  crucial than structures. You can write perfectly good object-oriented programs in C++ without  knowing anything about enumerations. However, they are very much in the spirit of C++, in  that, by allowing you to define your own data types, they can simplify and clarify your programming
  • 3.
    Enumeration  Enum inC++ is a data type that contains fixed set of constants.  It can be used for days of the week (SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY and SATURDAY) , directions (NORTH, SOUTH, EAST and WEST) etc. The C++ enum constants are static and final implicitly.  C++ Enums can be thought of as classes that have fixed set of constants.
  • 4.
    Points to rememberfor C++ Enum  enum improves type safety  enum can be easily used in switch  enum can be traversed  enum can have fields, constructors and methods  enum may implement many interfaces but cannot extend any class because it internally extends Enum class
  • 5.
    Days of theWeek Enumeration
  • 6.
    C++ Enumeration Example 1.#include <iostream> 2. using namespace std; 3. enum week { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };
  • 7.
    C++ Enumeration Example 4.int main() 5. { 6. week day; 7. day = Friday; 8. cout << "Day: " << day+1<<endl; 9. return 0; 10. } Output: Day: 5
  • 8.
    C++ Enumeration Example2 #include <iostream> using namespace std; //specify enum type enum days_of_week { Sun, Mon, Tue, Wed, Thu, Fri, Sat }; int main() { days_of_week day1, day2; //define variables //of type days_of_weekint main() { days_of_week day1, day2; //define variables //of type days_of_week
  • 9.
    C++ Enumeration Example2 day1 = Mon; //give values to day2 = Thu; //variables int diff = day2 - day1; //can do integer arithmetic cout << “Days between = “ << diff << endl; if(day1 < day2) //can do comparisons cout << “day1 comes before day2n”; return 0; }
  • 10.
    Explanation  An enumerationis a list of all possible values. This is unlike the specification of an int, for example, which is given in terms of a range of values. In an enum you must give a specific name to every possible value.
  • 11.
    Difference between anint and an enum.