C++ Enumeration

C++ Enumeration

In this article, you will learn to work with enumeration (enum). Likewise, you will realize where enums are normally used in C++ programming.

An enumeration is a user-defined data type that comprises of necessary constants. To characterize a list, a watchword enum is used.

enum season { spring, summer, autumn, winter };

Here, the name of the enumeration is season.

And, spring, summer and winter are values of type season.

By default, spring is 0, summer is 1 and so on. You can change the default value of an enum element during declaration (if necessary).

enum season 
{   spring = 0, 
    summer = 4, 
    autumn = 8,
    winter = 12
};

Enumerated Type Declaration

At the point when you make an enumerated type, just blueprint for the variable is made. Here’s the manner by which you can make variables of the enum type.

enum boolean { false, true };

// inside function
enum boolean check;

Here, a variable check of type enum boolean is created.

Here is another way to declare same check variable using different syntax.

enum boolean 
{ 
   false, true
} check;

Example 1: Enumeration Type

#include <iostream>
using namespace std;

enum week { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };

int main()
{
    week today;
    today = Wednesday;
    cout << "Day " << today+1;
    return 0;
}

Output

Day 4

Example2: Changing Default Value of Enums

#include <iostream>
using namespace std;

enum seasons { spring = 34, summer = 4, autumn = 9, winter = 32};

int main() {

    seasons s;

    s = summer;
    cout << "Summer = " << s << endl;

    return 0;
}

Output

Summer = 4

Why enums are used in C++ programming?

An enum variable takes only one value out of many possible values. Example to demonstrate it,

#include <iostream>
using namespace std;

enum suit {
    club = 0,
    diamonds = 10,
    hearts = 20,
    spades = 3
} card;

int main() 
{
    card = club;
    cout << "Size of enum variable " << sizeof(card) << " bytes.";   
    return 0;
}

Output

Size of enum variable 4 bytes.

This is on the grounds that the size of a whole number is 4 bytes.;

This makes on enum a decent decision to work with banners.

You can achieve a similar assignment using C++ structures. In any case, working with enums gives you effectiveness alongside adaptability.


How to use enums for flags?

Let us take an example,

enum designFlags {
	ITALICS = 1,
	BOLD = 2,
	UNDERLINE = 4
} button;

Suppose you are designing a button for Windows application. You can set flags ITALICS, BOLD and UNDERLINE to work with text.

There is a reason why all the integral constants are power of 2 in above pseudocode.

// In binary

ITALICS = 00000001
BOLD = 00000010
UNDERLINE = 00000100 

Since the necessary constants are the intensity of 2, you can join at least two banners immediately without covering using bitwise OR | operator. This allows you to pick at least two flags immediately. For instance,

#include <iostream>
using namespace std;

enum designFlags {
    BOLD = 1,
    ITALICS = 2,
    UNDERLINE = 4
};

int main() 
{
    int myDesign = BOLD | UNDERLINE; 

        //    00000001
        //  | 00000100
        //  ___________
        //    00000101

    cout << myDesign;

    return 0;
}

Output

5

At the point when the output is 5, you generally realize that striking and underline is used.

Additionally, you can add flag to your necessities.

if (myDesign & ITALICS) {
    // code for italics
}

Here, we have added italics to our plan. Note, possibly code for italics is composed inside the if statement.

You can achieve nearly anything in C++ programming without using enumerations. Be that as it may, they can be pretty handy in specific circumstances. That is the thing that separates good programmers from great programmers.


Please feel free to give your comment if you face any difficulty here.

Leave a Reply

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