What are enum, and union?

Professor posted a code for us to look at it but I am having a hard time understanding what enum and union are can someone explain please and thank you :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  #include <iostream>
using namespace std;

enum paytype { hourly, salary };

union payrate {
    float per_hour; // hourly worker
    // assume hourly worker paid on 40 hour week
    int per_month; // salary worker
};

struct Employee {
    int empNumber;
    char name[80];
    paytype type;
    payrate rate;
};

void input(Employee*, int);
void output(Employee[], int);

int main()
{
    const int MAX = 2;
    Employee emp[MAX];
    input(emp, MAX);
    output(emp, MAX);
    return 0;
}

void input(Employee* e, int num)
{
    char ch;
    for (int i = 0; i < num; i++)
    {
        cout << "Enter the employee's number: ";
        cin >> e[i].empNumber;
        cin.ignore();
        cout << "Enter the employee's name: ";
        cin.getline(e[i].name, 80);
        cout << "Hourly or salaried (H/h or S/s) : ";
        cin >> ch;
        if (ch == 'h' || ch == 'H')
        {
            e[i].type = hourly;
            cout << "Hourly rate: ";
            cin >> e[i].rate.per_hour;
        }
        else
        {
            e[i].type = salary;
            cout << "Monthly salary: ";
            cin >> e[i].rate.per_month;
        }
    }
}
 

void output(Employee e[], int num)
{
    cout << "\nHere is the employee payroll data: \n";
    for (int i = 0; i < num; i++)
    {
        cout << "Name: " << e[i].name << endl;
        cout << "Number: " << e[i].empNumber << endl;
        if (e[i].type == hourly)
        {
            cout << "Hourly rate: $" << e[i].rate.per_hour << endl;
            cout << "Total pay: $" << e[i].rate.per_hour * 40 << endl;
        }
        else
            cout << "Salary: $" << e[i].rate.per_month << endl;
    }
}
The "down and dirty" of both the union and enum

A union, in almost all regards, is just like a structure. The difference is that all the members
of a union use the same memory area, so only one member can be used at a time. A union
might be used in an application where the program needs to work with two or more values
(of different data types), but only needs to use one of the values at a time. Unions conserve memory by storing all their members in the same memory location. Unions are declared just like structures, except the key word union is used instead of
struct . Here is an example:

1
2
3
4
5
union PaySource
{
short hours;
float sales;
};


A union variable of the data type shown above can then be defined as PaySource employee1; The PaySource union variable defined here has two members: hours (a short ), and sales (a float ). The entire variable will only take up as much memory as the largest member (in this case, a float ).

Using the enum key word you can create your own data type and specify the values that
belong to that type. Such a type is known as an enumerated data type . Here is an example of an enumerated data type declaration:
enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY };
An enumerated type declaration begins with the key word enum , followed by the name of
the type, followed by a list of identifiers inside braces, and is terminated with a semicolon.
The example declaration creates an enumerated data type named Day . The identifiers
MONDAY , TUESDAY , WEDNESDAY , THURSDAY , and FRIDAY , which are listed inside the braces, are known as enumerators. So just what are these enumerators MONDAY , TUESDAY , WEDNESDAY , THURSDAY , and FRIDAY ? You can think of them as integer named constants. Internally, the compiler assigns integer values to the enumerators, beginning with 0. The enumerator MONDAY is stored in memory as the number 0, TUESDAY is stored in memory as the number 1, WEDNESDAY is stored in memory as the number 2, and so forth. To prove this, look at the following code:
1
2
3
cout << MONDAY << endl << TUESDAY << endl
<< WEDNESDAY << endl << THURSDAY << endl
<< FRIDAY << endl;

The output will be:
0
1
2
3
4
Thanks chicofeo that makes sense :D
Good deal!
Topic archived. No new replies allowed.