Enumeration

Estimated reading: 4 minutes 25 Views

Introduction

In C++, an enumeration, also known as enumerated type, is a user-defined data type that consists of a set of named integer constants. These constants are essentially representative names assigned to integer values.

Declaration:

  • The enum keyword is used to declare an enumeration.
  • You list the constant names separated by commas, optionally enclosed in curly braces.
  • By default, constants start with a value of 0 and increment by 1 for each subsequent one.
  • You can explicitly assign specific integer values to individual constants.

Example 1:

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
enum days {sat = 1, sun, mon, tue, wed, thu, fri};
int main()
{
    string d[7] = {"sat", "sun", "mon", "tue", "wed", "thu", "fri"};
    days m[7] = {sat, sun, mon, tue, wed, thu, fri};
    for (size_t i = 0; i < 7; i++)
    {
        cout << m[i] << " - " << d[i] << endl;
    }
    return 0;
}

Enumeration days:

  • Declares an enumeration named days to represent the days of the week.
  • Assigns values to each day: sat = 1, sun, mon, etc. (subsequent days get implicit values incremented from the previous one).

String Array d:

  • Creates an array of strings named d with a size of 7, holding the full day names (“sat”, “sun”, …, “fri”).

Enumeration Array m:

  • Creates an array of enumeration values named m with a size of 7, containing the day enumerations (sat, sun, …, fri).

for Loop:

  • Iterates through the elements of both d and m using a single index i.
  • For each iteration:
    – Prints the current enumeration value from m[i] followed by a hyphen (“-“).
    – Prints the corresponding day name from d[i] on the same line.

Example 2:

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
string da[7] = {"sat", "sun", "mon", "tue", "wed", "thu", "fri"};
enum Days {sat = 1, sun, mon, tue, wed, thu, fri};
class week {
        Days d[7];
    public:
        void setday(Days w[])
        {
            for (size_t i = 0; i < 7; i++)
                d[i] = w[i];
        }
        void p()
        {
            for (size_t i = 0; i < 7; i++)
                cout << "The number of day " <<  da[i] << " = " << d[i] << endl;
        }
};
int main()
{
    Days d[7] = {sat, sun, mon, tue, wed, thu, fri};
    week g;
    g.setday(d);
    g.p();
    return 0;
}

Array da:

  • Declares a global string array da of size 7, storing the full day names (“sat”, “sun”, …, “fri”).

Enumeration Days:

  • Defines an enumeration named Days to represent the days of the week with integer values:
    – sat = 1
    – sun (implicitly 2)
    – mon (implicitly 3)
    – …
    – fri (implicitly 7)

Class week:

  • Represents a “week” concept.
    – Member variable:
    * d is an array of Days enumeration type, holding the numerical day values for a week (e.g., sat, sun, …).
    – Member methods:
    * setday(Days w[]): Takes an array of Days enumeration values as input and assigns them to the d member array, copying the day values into the object.
    * p(): Prints the day names and their corresponding numerical values from the d array. It iterates through the array, accessing both the current da string and the d value at the same index to print the pair.

main Function:

  • Creates an array d of Days type and initializes it with sat, sun, …, fri.
  • Instantiates a week object named g.
  • Calls the setday method of g to pass the d array (containing the day values) to the object.
  • Calls the p method of g, which prints the day information in the format “The number of day <day name> = <day value>”.

The code demonstrates the use of enumerations and classes to represent and manage day-of-the-week information in C++.

Share this

Enumeration

Or copy link

CONTENTS
English