C++ Programming

C++ Enumeration

 

In C++, enumeration (enum) is a user-defined data type that consists of named integral constants. Enums are useful for creating more readable and self-explanatory code by assigning names to numeric values.

Here’s a basic example of how to use enums in C++:

#include <iostream.h>

// Define an enumeration named Color

enum Color {

    RED,    // 0

    GREEN,  // 1

    BLUE    // 2

};

int main() {

    // Declare a variable of type Color

    Color myColor = RED;

    // Use the variable

    if (myColor == RED) {

        cout << “The color is red.” << endl;

    } else if (myColor == GREEN) {

        cout << “The color is green.” << endl;

    } else if (myColor == BLUE) {

        cout << “The color is blue.” << endl;

    }

    return 0;

}

In this example, the Color enum has three named constants: RED, GREEN, and BLUE. By default, these constants are assigned integral values starting from 0. You can explicitly assign values to enum constants if needed.

Enums are often used to improve code readability and maintainability by giving meaningful names to numeric values. They are commonly used in switch statements, function parameters, and other situations where a set of related constant values is required.

C++ Enumeration example

#include <iostream.h>

// Define an enumeration named Day

enum Day {

    SUNDAY,    // 0

    MONDAY,    // 1

    TUESDAY,   // 2

    WEDNESDAY, // 3

    THURSDAY,  // 4

    FRIDAY,    // 5

    SATURDAY   // 6

};

// Function that takes a Day enum as a parameter

void printDay(Day day) {

    switch (day) {

        case SUNDAY:

            cout << “Sunday” << endl;

            break;

        case MONDAY:

            cout << “Monday” << endl;

            break;

        // … (cases for other days)

        default:

            cout << “Invalid day” << endl;

            break;

    }

}

int main() {

    // Use the function with an enum parameter

    printDay(WEDNESDAY);

    return 0;

}

In this example, the Day enum is used to represent the days of the week, and the printDay function demonstrates how to use enums in switch statements.

Run

Alt+f9

Ctr+f9

Alt+f5

Recommended Posts

C++ Programming

Visibility modes in C++

In C++, visibility modes refer to the accessibility of class members (such as variables and functions) from different parts of a program. C++ provides three visibility modes: public, private, and protected. These modes control the access levels of class members concerning the outside world and derived classes. Public: Members declared as public are accessible from […]

Rekha Setia 
C++ Programming

Inheritance in C++

In C++, inheritance is a fundamental concept of object-oriented programming (OOP) that allows you to create a new class based on an existing class, known as the base or parent class. The new class is called the derived or child class. Inheritance facilitates code reuse and supports the creation of a hierarchy of classes. There […]

Rekha Setia 
C++ Programming

C++ Classes and Objects

In C++, classes and objects are fundamental concepts that support object-oriented programming (OOP). Here’s a brief overview of classes and objects in C++: Classes: In C++, a class is a user-defined data type that allows you to encapsulate data members and member functions into a single unit. Classes are the building blocks of object-oriented programming […]

Rekha Setia 

Leave A Comment