Course Content
C++ Introduction
0/1
C++ Variables & Constants
0/1
C++Scope of Variable
0/1
C++ Keywords & Identifiers
0/1
C++ Data Types
0/1
C++ Basic I/O
0/1
C++ Type Conversion
0/1
C++ Operators
0/1
C++ Comments
0/1
C++ If-else
0/1
C++ Ternary Operator
0/1
C++ for Loop
0/1
C++ Ranged for Loop
0/1
C++ while/do-while Loop
0/1
C++ break Statement
0/1
C++ Continue Statement
0/1
C++ switch Statement
0/1
C++ goto Statement
0/1
C++ Functions
0/1
C++ User-defined Functions
0/1
C++ Default Arguments
0/1
C++ Storage Class
0/1
C++ Recursion
0/1
C++ Return by Reference
0/1
C++ Arrays
0/1
C++ Multi-dimentional Arrays
0/1
C++ Arrays & Function
0/1
C++ String
0/1
C++ Structure
0/1
C++ Structure & Functions
0/1
C++ Pointers to Structure
0/1
C++ Pointers
0/1
C++ Void Pointers
0/1
C++ Pointers & Arrays
0/1
C++ Pointers & Functions
0/1
C++ Dynamic Memory Allocation
0/1
C++ OOPs Concepts
0/1
C++ Objects and Class
0/1
C++ Constructors
0/1
C++ Destructors
0/1
C++ Constructor Overloading
0/1
C++ Objects & Function
0/1
C++ Enumeration
0/1
C++ Inheritance
0/1
C++ Inheritance Access Control
0/1
C++ Inheritance Types
0/1
C++ Polymorphism
0/1
C++ Function Overloading
0/1
C++ Function Overriding
0/1
C++ Operator Overloading
0/1
C++ Friend Function
0/1
C++ Virtual Function
0/1
C++ Abstract Class & Pure Virtual Function
0/1
C++ Encapsulation
0/1
C++ Abstraction
0/1
C++ Templates
0/1
C++ Exception Handling
0/1
C++ Multithreading
0/1
C++ Standard Library
0/1
C++ Programming Tutorials
About Lesson

C++ Enumeration

In this tutorial, we will learn to work with enumeration (enum). Also, you will learn where enums are commonly used in C++ programming with the help of examples.

Enumeration

  • Enumerations are a user-defined data types which can be assigned some limited values.
  • In this, you can specify a set of values for a variable and the variable can select only one of the set. It has a fixed set of constants.
  • To define an enumeration, keyword ‘enum’ is used.
  • These values are defined by the programmer at the time of declaring the enumerated type.
  • Enumerations can be used to define days of a week, months, weathers, etc. The enum constants are static and final implicitly.

Defining an Enum

An enum is defined in the same way as structure with the keyword struct replaced by the keyword enum and the elements seperated by ‘comma’ as follows

Syntax

enum enum_name{ element1, element2, element3, element4,};

Now let’s define an enum of the above example of seasons.

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

Here, the name of the enumeration is season.

And,winterspringautumn, and summer are values of type season.

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

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

Enumerated Type Declaration

When you create an enumerated type, only blueprint for the variable is created. Here’s how you can create variables of enum type.

enum season { winter, autumn,spring, summer};
                                                          
int main(){
enum Seasons s;
return 0;
}

Here s is the variable of the enum named Season. This variable will represent a season.

We can also declare an enum variable as follows:

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

Another Example:

enum boolean { false, true };

int main(){
enum boolean check;
return 0;
}

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: C++ Program to find direction using Enumeration

#include <iostream>
using namespace std;

enum direction { North, South, East, West };
int main(){
    direction dir;
    dir = East;
    cout << "Direction: " << dir;
    return 0;
}

Output

Day 4

Example 2: C++ Program to find week days using Enumeration

#include <iostream>
using namespace std;
enum week { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
int main(){
    week today;
    today = Monday;
    cout << "Day " << today+1;
    return 0;
}

Output

Day 2

Example 3: C++ Changing Default Value of Enums

To print the content of a void pointer, we use the static_cast operator. It converts the pointer from void* type to the respective data type of the address the pointer is storing:

#include <iostream>
using namespace std;

enum seasons { spring = 12, summer = 13, autumn = 14, winter = 11};
int main() {
    seasons s;
    s = Autumn;
    cout << "Summer = " << s << endl;
                                                                      
    return 0;
}

Output

Autumn = 14

Why enums are used in C++ programming?

Enums are used only when we expect the variable to have one of the possible set of values.
For example:-

#include <iostream>
using namespace std;
enum direction { East = 5, West = 10, North = 15, South = 20 } dir;
int main() {
    dir = North;
    cout << "Size of enum variable " << sizeof(North) << " bytes.";   
    return 0;
}

Output

Size of enum variable 4 bytes.

It’s because the size of an integer is 4 bytes. This makes enum a good choice to work with flags.

You can accomplish the same task using C++ structures. However, working with enums gives you efficiency along with flexibility.


How to use enums for flags?

Let understand with an example:

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

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

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

// In binary

UNDERLINE = 00000001
ITALICS = 00000010
BOLD = 00000100

Since, the integral constants are power of 2, you can combine two or more flags at once without overlapping using bitwise OR | operator. This allows you to choose two or more flags at once.
For example:-

#include <iostream>
using namespace std;
enum printFlags { BOLD = 1, ITALICS = 2, UNDERLINE = 4};
int main() {
    int myFlag = UNDERLINE | BOLD ; 

        //    00000001
        //  | 00000100
        //  ___________
        //    00000101
    cout << "Displaying myFlag = " <<  myFlag;

    return 0;
}

Output

Displaying myFlag = 5

When the output is 5, you always know that underline and bold is used.

Also, you can add flag to your requirements.

if (myFlag & ITALICS) {
    // statement for italics
}

Here, we have added italics to our design. Note, only code for italics is written inside the if statement. You can accomplish almost anything in C++ programming without using enumerations. However, they can be pretty handy in certain situations. That’s what differentiates good programmers from great programmers.


Advantages of Enumeration

Some of the advantages of enum are:

1. Enum can be used in switch case.
2. Enum improves type safety.
3. Enum can be traversed.
4. Enum can have fields, constructors and methods.
5. Enum may implement many interfaces but cannot extend any class because it internally extends Enum class.

Exercise Files
No Attachment Found
No Attachment Found
error: Content is protected !!