Enumerated Constants

C++ also allows for enumerated constants.
This means the programmer can create a new variable type and then assign a finite number of values to it.

The syntax of the enum keyword:-

enum type_name {
  value1,
  value2,
  value3,
  .
  .
} object_names;
  • For example:
 enum MONTH {Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};


//OUTPUT:-
// Jan = 0
// Feb = 1
// etc.

In this example the enum variable MONTH has twelve possible values. These 12 values translate into 12 integer values.


The program below demonstrates the use of the enum variable.

/*Enum example*/

#include <iostream>

using namespace std;

int main()
{
    //define MONTH as having 12 possible values
    enum MONTH {Jan, Feb, Mar, Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec};
    
    //define bestMonth as a variable type MONTHS
    MONTH bestMonth;
    
    //assign bestMonth one of the values of MONTHS
    bestMonth = Jan;
    
    //now we can check the value of bestMonths just 
    //like any other variable
    if(bestMonth == Jan)
    {
        cout<<"I'm not so sure January is the best month\n";
    }
    return 0;
}


//OUTPUT:-
I'm not so sure January is the best month

Note

we can read more about enumerated variables at C++ Data Types