Hi there, we’re Harisystems

"Unlock your potential and soar to new heights with our exclusive online courses! Ignite your passion, acquire valuable skills, and embrace limitless possibilities. Don't miss out on our limited-time sale - invest in yourself today and embark on a journey of personal and professional growth. Enroll now and shape your future with knowledge that lasts a lifetime!".

For corporate trainings, projects, and real world experience reach us. We believe that education should be accessible to all, regardless of geographical location or background.

1
1

C++ Enumeration

In C++, enumeration, or enum, is a user-defined data type used to represent a set of named constant values. Enumerations provide a way to define and work with a fixed set of related values, improving code readability and maintainability. In this article, we will explore the usage of enumerations in C++ with examples.

1. Defining and Using Enumerations

To define an enumeration in C++, you use the enum keyword followed by the enumeration name and a list of named constants. Here's an example:

#include <iostream>

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

int main() {
    Day today = Wednesday;

    if (today == Wednesday) {
        std::cout << "Today is Wednesday." << std::endl;
    }

    return 0;
}

In the above code, we define an enumeration named Day with seven named constants representing the days of the week. We then declare a variable today of type Day and assign it the value Wednesday. We use an if statement to check if today is equal to Wednesday and output a message accordingly.

2. Assigning Values to Enumerators

By default, enumerators in C++ are assigned values starting from 0. However, you can assign specific values to enumerators. Here's an example:

#include <iostream>

enum Month {
    January = 1,
    February,
    March,
    April,
    May,
    June,
    July,
    August,
    September,
    October,
    November,
    December
};

int main() {
    Month birthMonth = July;

    std::cout << "Birth month: " << birthMonth << std::endl;

    return 0;
}

In the above code, we define an enumeration named Month with twelve named constants representing the months of the year. We explicitly assign the value 1 to January, and the subsequent enumerators get assigned values automatically incremented by 1. We declare a variable birthMonth of type Month and assign it the value July. We then output the value of birthMonth to the console.

3. Enumerations with Scoped Names

By default, enumerations in C++ are not scoped. However, you can make them scoped using the enum class syntax. Here's an example:

#include <iostream>

enum class Color {
    Red,
    Green,
    Blue
};

int main() {
    Color favoriteColor = Color::Blue;

    switch (favoriteColor) {
        case Color::Red:
            std::cout << "Your favorite color is Red." << std::endl;
            break;
        case Color::Green:
            std::cout << "Your favorite color is Green." << std::endl;
            break;
        case Color::Blue:
            std::cout << "Your favorite color is Blue." << std::endl;
            break;
    }

    return 0;
}

In the above code, we define a scoped enumeration named Color with three named constants representing colors. We declare a variable favoriteColor of type Color and assign it the value Color::Blue. We use a switch statement to check the value of favoriteColor and output a corresponding message.

Enumerations provide a convenient way to represent a fixed set of related values in C++. They improve code readability, help catch errors at compile-time, and provide a clear and concise way to work with named constants. Utilize enumerations to enhance the structure and semantics of your C++ code.

4.5L

Learners

20+

Instructors

50+

Courses

6.0L

Course enrollments

4.5/5.0 5(Based on 4265 ratings)

Future Trending Courses

When selecting, a course, Here are a few areas that are expected to be in demand in the future:.

Beginner

The Python Course: Absolute Beginners for strong Fundamentals

By: Sekhar Metla
4.5 (13,245)
Intermediate

JavaScript Masterclass for Beginner to Expert: Bootcamp

By: Sekhar Metla
4.5 (9,300)
Intermediate

Python Coding Intermediate: OOPs, Classes, and Methods

By: Sekhar Metla
(11,145)
Intermediate

Microsoft: SQL Server Bootcamp 2023: Go from Zero to Hero

By: Sekhar Metla
4.5 (7,700)
Excel course

Future Learning for all

If you’re passionate and ready to dive in, we’d love to join 1:1 classes for you. We’re committed to support our learners and professionals their development and well-being.

View Courses

Most Popular Course topics

These are the most popular course topics among Software Courses for learners