Enumeration in C++

Enumeration in C++

Enumeration in C++ is a user-defined data type that consists of set of named integral constants. For define an enumeration enum keyword is used.

Syntax of enumeration in C++

enum enumerated-type-name{value1, value2, value3…..valueN};

Enumeration used for days of the week as well as directions.

C++ Enums is classes that have fixed set of constants.

For example

enum dir { north, south, east, west };

Enumerated Type Declaration

When you create an enumerated type, created only blueprint for the variable

enum boolean { false, true };

// inside function
enum boolean check;

Program of enumeration in C++

#include <iostream>
using namespace std;

enum month { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec )

int main()
{
    Month today;
    today = Mar;
    cout << "Month " << today+1;
    return 0;
}

Output

Month 3

 

Read more, Pointers to Structure in C++

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *