Enums

A user-defined data type is an enum or enumeration. Named constants in enums indicate integral values. Enums are used in programmes to make them more understandable and less complex. It allows us to specify a fixed set of possible values and then define variables that have one of those values.

Creating an Enum element

To define the enum, we utilise the enum keyword.

The syntax for defining a union is as follows:

enum enum_name
{
    element1,
    element2,
    element3
};

Here’s an example of how to define and use a union as a user-defined data type in main.

enum Meal
{
    breakfast,
    lunch,
    dinner
};

Initialising and using enum elements

Because each enum member is assigned a value, they can be used to determine if two variables store the same value.

#include <iostream>
using namespace std;
 
enum Meal
{
    breakfast,
    lunch,
    dinner
};
 
int main()
{
    Meal m1 = dinner;
    if (m1 == 2)
    {
        cout << "The value of dinner is " << dinner << endl;
    }
}

Output:

The value of dinner is 2
Shubhajna Rai
Shubhajna Rai

A Civil Engineering Graduate interested to share valuable information with the aspirants.

Leave a Reply

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

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in