Enumeration in C++:


Enumeration, also known as Enumerated type, is a custom data type in C++ that allows the programmer to define a set of named constants. These constants represent the limited values that the enumeration type can hold.

When assigning values to enumerated types, similar to assigning a float value to a character, attempting to assign incompatible values leads to compiler errors. Enumerators, the values within an enumeration, are akin to constants and are assigned integer values by default, starting from 0. These enumerators can be utilized alongside switch statements.

enum season { 
  spring, 
  summer, 
  autumn, 
  winter
};

In this syntax, "season" is the name of the enumeration, and spring, summer, autumn, and winter are its values.

The final item in the list does not require a comma.

By default, if not specified explicitly, spring is assigned 0, summer is assigned 1, and so forth. You can alter these default values during declaration:

enum season { 
  spring = 1, 
  summer = 13, 
  autumn = 54, 
  winter = 12
};

Creating an enumeration establishes a blueprint for the variable type. Here's how we declare variables of an enum type:

enum boolean { false, true };

// Inside a function
enum boolean check;

In the above example, a variable check of type enumboolean is created. Alternatively, the same variable check can be declared using different syntax:

enum boolean 
{ 
   false, true
} check;

Let's explore an example where we define a variable named "week" with an enumtype. The expected output of this program is "Day 4". This is because today is Wednesday, equivalent to day 3 when counting from 0. Therefore, adding 1 to today's index should yield 4. To understand how enum types operate, copy and run the code yourself.

C++ Copy to Clipboard  
#include <iostream>
using namespace std;

enum week { Sunday, Monday, Tuesday, 
            Wednesday, Thursday, Friday, Saturday              
};

int main() 
{
    week today;
    today = Wednesday;
    cout << "Day " << today + 1;
    return 0;
}
  • The weekenum is defined with values representing each day of the week, starting from 0.
  • In the mainfunction, the variable "today" of type "week" is declared.
  • It's then assigned the value "Wednesday", which corresponds to an enum value of 3.
  • Finally, it prints "Day" followed by today + 1, which results in 4 (since today holds the value Wednesday, which is 3, and adding 1 gives 4).

Enums are frequently employed in switch statementsto assess corresponding values:

Consider the following example where we defines an enumeration Level with constants LOW, MEDIUM, and HIGH. It then declares a variable myVarof type "Level" and assigns it the value MEDIUM. Finally, it uses a switch statementto print out the corresponding level based on the value of myVar.

C++ Copy to Clipboard  
#include <iostream>
using namespace std;

enum Level {
  LOW = 1,
  MEDIUM,
  HIGH
};

int main() {
  enum Level myVar = MEDIUM;

  switch (myVar) {
    case LOW:
      cout << "Low Level";
      break;
    case MEDIUM:
      cout << "Medium level";
      break;
    case HIGH:
      cout << "High level";
      break;
  }
  return 0;
}