C++ Enumeration

The enumeration in C++ is a user-defined data type that consists of integral constants or a fixed set of constants. 

The keyword enum is used to define Enumeration. 

Syntax : 

enum name-of-enumeration{ v1,v2,...vN};

where v1,v2,.. vN are values of type name-of-enumeration. By default the value of v1 is 0, v2 is 2, and so on. 

Example:

enum months {Jan , Feb , Mar , Apr , May , June , July , Aug ,Sept ,Nov ,Dec}; 

In the above example, the value assigned to Jan is 0, Feb is 1, Mar is 3, and so on.

DECLARATION 

For declaration of Enum variable, the name of enumeration along with the enum variable is written. 

Syntax 1:

enum months {Jan , Feb , Mar , Apr , May , June , July , Aug ,Sept ,Nov ,Dec}; 
Int main(){
months m; //m is the enum variable. 
return 0; 
} 

Syntax 2:

enum months {Jan , Feb , Mar , Apr , May , June , July , Aug ,Sept ,Nov ,Dec} m; 

Example 1(Enum With Default Values): 

Code:

C++ Code

#include <iostream> 
using namespace std; 
enum months {Jan , Feb , Mar , Apr , May , June , July , Aug ,Sept ,Nov ,Dec}; 
int main(){ 
months thismonth; 
thismonth = Apr; 
cout << "This is month number " << thismonth+1; return 0; 
} 

Output:

This is month number 4

Example 2(Enum With Changed Values):

Code:

C++ Code

#include <iostream> 
using namespace std; 
enum questionssolved {Arrays =12,Stacks= 10,Queues= 12,Graph =23 , Trees =19}qstn; 
int main(){ 
qstn = Stacks; 
cout<<"The Number of Questions solved from topic Stacks is: "<<qstn; 
} 

Output: The Number of Questions solved from topic Stacks is: 10

Special thanks to Naman Mathur for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article