CPP Enumeration (enum)

Introduction

Enumerations (enums) in C++ are user-defined data types that consist of a set of named integer constants. Enums are used to assign names to integral constants, making a program easier to read and maintain. They are especially useful when a variable can take one of a limited set of possible values.

Defining and Declaring Enumerations

Syntax for Defining an Enumeration

enum EnumName {
    CONSTANT1,
    CONSTANT2,
    // additional constants
};

Example: Defining and Declaring an Enumeration

#include <iostream>
using namespace std;

// Define an enumeration named 'Day'
enum Day {
    SUNDAY,
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY
};

int main() {
    // Declare an enum variable
    Day today;

    // Assign a value to the enum variable
    today = WEDNESDAY;

    // Print the value of the enum variable
    cout << "Day: " << today << endl; // Output will be the integer value of WEDNESDAY

    return 0;
}

Output

Day: 3

Explanation

  • enum Day defines an enumeration named Day with constants representing the days of the week.
  • Day today; declares a variable of type Day.
  • today = WEDNESDAY; assigns the constant WEDNESDAY to the variable today.
  • The integer value associated with WEDNESDAY (which is 3) is printed.

Assigning Specific Values to Enum Constants

By default, the values of enum constants start from 0 and increment by 1. You can assign specific values to enum constants.

Example: Assigning Specific Values

#include <iostream>
using namespace std;

// Define an enumeration named 'Month' with specific values
enum Month {
    JANUARY = 1,
    FEBRUARY,
    MARCH,
    APRIL,
    MAY,
    JUNE,
    JULY,
    AUGUST,
    SEPTEMBER,
    OCTOBER,
    NOVEMBER,
    DECEMBER
};

int main() {
    // Declare an enum variable
    Month currentMonth;

    // Assign a value to the enum variable
    currentMonth = JULY;

    // Print the value of the enum variable
    cout << "Month: " << currentMonth << endl; // Output will be the integer value of JULY

    return 0;
}

Output

Month: 7

Explanation

  • enum Month defines an enumeration named Month with constants representing the months of the year.
  • Specific values are assigned to the constants, starting from 1 for JANUARY.
  • The constant JULY has the value 7, which is printed.

Using Enums in Switch Statements

Enums can be used in switch statements to execute different code based on the enum value.

Example: Using Enums in Switch Statements

#include <iostream>
using namespace std;

// Define an enumeration named 'TrafficLight'
enum TrafficLight {
    RED,
    YELLOW,
    GREEN
};

int main() {
    // Declare an enum variable
    TrafficLight light;

    // Assign a value to the enum variable
    light = YELLOW;

    // Use the enum variable in a switch statement
    switch (light) {
        case RED:
            cout << "Stop!" << endl;
            break;
        case YELLOW:
            cout << "Caution!" << endl;
            break;
        case GREEN:
            cout << "Go!" << endl;
            break;
        default:
            cout << "Invalid traffic light color!" << endl;
    }

    return 0;
}

Output

Caution!

Explanation

  • enum TrafficLight defines an enumeration named TrafficLight with constants RED, YELLOW, and GREEN.
  • The light variable is assigned the value YELLOW.
  • The switch statement executes the code corresponding to the value of light.

Example Programs

Example 1: Days of the Week

This example demonstrates using an enum to represent the days of the week and printing a specific day’s name.

#include <iostream>
using namespace std;

enum Day {
    SUNDAY,
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY
};

int main() {
    Day today = FRIDAY;

    switch (today) {
        case SUNDAY:
            cout << "Today is Sunday" << endl;
            break;
        case MONDAY:
            cout << "Today is Monday" << endl;
            break;
        case TUESDAY:
            cout << "Today is Tuesday" << endl;
            break;
        case WEDNESDAY:
            cout << "Today is Wednesday" << endl;
            break;
        case THURSDAY:
            cout << "Today is Thursday" << endl;
            break;
        case FRIDAY:
            cout << "Today is Friday" << endl;
            break;
        case SATURDAY:
            cout << "Today is Saturday" << endl;
            break;
        default:
            cout << "Invalid day" << endl;
    }

    return 0;
}

Output

Today is Friday

Explanation

  • The Day enum is used to represent the days of the week.
  • The switch statement prints the name of the day based on the value of the today variable.

Example 2: Seasons of the Year

This example demonstrates using an enum to represent the seasons of the year and printing a message based on the current season.

#include <iostream>
using namespace std;

enum Season {
    WINTER,
    SPRING,
    SUMMER,
    AUTUMN
};

int main() {
    Season currentSeason = SUMMER;

    switch (currentSeason) {
        case WINTER:
            cout << "It's cold outside!" << endl;
            break;
        case SPRING:
            cout << "Flowers are blooming!" << endl;
            break;
        case SUMMER:
            cout << "It's hot and sunny!" << endl;
            break;
        case AUTUMN:
            cout << "Leaves are falling!" << endl;
            break;
        default:
            cout << "Invalid season" << endl;
    }

    return 0;
}

Output

It's hot and sunny!

Explanation

  • The Season enum is used to represent the seasons of the year.
  • The switch statement prints a message based on the value of the currentSeason variable.

Conclusion

Enumerations (enums) in C++ provide a way to define named integer constants, making code more readable and maintainable. This chapter covered how to define, declare, and initialize enums, including assigning specific values to enum constants. It also demonstrated using enums in switch statements and provided example programs to illustrate their usage. Understanding how to use enums effectively will help you write clearer and more organized code. In the next chapter, we will explore file handling in C++.

Leave a Comment

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

Scroll to Top