C++ Enumeration

A user-defined data type called C++ Enumeration gives names to integral constants. It gives the programmer the ability to make a collection of named integer constants that stand in for a set of connected values.

Basic syntax of Enumeration:

enum enumeration_name {
   value1,
   value2,
   value3,
   ...
};

Here, enumeration_name is the name of the enumeration, and value1, value2, value3, and so on are the named integer constants associated with the enumeration.

Example:

#include <iostream>
using namespace std;

enum Color {
   RED,
   GREEN,
   BLUE
};

int main() {
   Color c = RED;
   
   if (c == RED) {
      cout << "Color is red" << endl;
   } else if (c == GREEN) {
      cout << "Color is green" << endl;
   } else {
      cout << "Color is blue" << endl;
   }
   
   return 0;
}

Output:

Color is red

In this example, the named constants RED, GREEN, and BLUE are used to define the enumeration Color. Following that, the program declares a Color variable of type c and gives it the value RED. In order to calculate the value of c and print the corresponding colour to the console, the program employs a conditional statement.