Enumeration, commonly known as enum, is a powerful feature in C++ that allows programmers to define a set of named constants. Enums provide a way to represent a group of related values, making code more readable and maintainable. This article will explore the concept of enumeration in C++, covering its definition and example.
What is Enumeration?
Enumeration in C++ is a user-defined data type that consists of a set of named constants, known as enumerators. Enums allow us to define corresponding values, making our code more expressive and self-documenting. Let's understand with one example.
Example:
enum Color {
RED,
GREEN,
BLUE
};
In this example, the enum "Color" represents different colors: RED, GREEN, and BLUE. By default, the enumerators are assigned integer values starting from 0, so RED will have a value of 0, GREEN will have a value of 1, and BLUE will have a value of 2.
Changing the Default value of the enum.
Enums in C++ assign default integer values starting from 0 for the first enumerator, 1 for the second, and so on. However, we can change these default values by explicitly assigning different values.
Example:
enum Color {
RED = 1,
GREEN,
BLUE
};
In this example, we change the default values of the Color enum by explicitly assigning RED as 1. The subsequent enumerators will be assigned the next integer values sequentially.
Enumerated Type Declaration.
Enumerated type declarations in C++ allow you to create a new type based on an existing enum. This enhances code clarity and type safety.
Example:
//C++ code example to Enumerated Type Declaration
#include <iostream>
using namespace std;
//declare enum
enum Weekday {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
};
typedef enum Weekday Day;
int main() {
Day today = WEDNESDAY;
cout << "Today is ";
switch (today) {
case MONDAY:
cout << "Monday";
break;
case TUESDAY:
cout << "Tuesday";
break;
case WEDNESDAY:
cout << "Wednesday";
break;
case THURSDAY:
cout << "Thursday";
break;
case FRIDAY:
cout << "Friday";
break;
case SATURDAY:
cout << "Saturday";
break;
case SUNDAY:
cout << "Sunday";
break;
default:
cout << "Invalid day";
break;
}
cout << endl;
return 0;
}
Output:
In this example, we define an enum named "Weekday" with seven enumerators representing different days of the week. We then use the typedef keyword to create a new type "Day" based on the "Weekday" enum.
Inside the main() function, we declare a variable "today" of type "Day" and assign it the value "WEDNESDAY". This is possible because we created the "Day" type based on the "Weekday" enum.
We then use a switch statement to display the corresponding day based on the value of "today". Depending on the value of "today", the switch case will execute and output the corresponding day of the week.
How to use Enum for Flags?
Enums can be used to define flag values, where each enumerator represents a specific flag that can be combined using bitwise operators.
Example:
//C++ program for using enum for flag
#include <iostream>
using namespace std;
enum Permissions {
READ = 1,
WRITE = 2,
EXECUTE = 4
};
int main() {
// Combine READ and WRITE flags
Permissions userPermissions = static_cast<Permissions>(READ | WRITE);
// Checking individual flags
if (userPermissions & READ) {
cout << "User has read permission" << endl;
}
if (userPermissions & WRITE) {
cout << "User has write permission" << endl;
}
if (userPermissions & EXECUTE) {
cout << "User has execute permission" << endl;
}
return 0;
}
Output:
User has read permission
User has write permission
Working:
In this example, we define an enum named "Permissions" with three enumerators: READ, WRITE, and EXECUTE. Each enumerator represents a specific permission or flag.
Inside the main() function, we declare a variable "userPermissions" of type "Permissions" and initialize it by combining the READ and WRITE flags using the bitwise OR operator (|).
To check individual flags, we use the bitwise AND operator (&) in combination with the respective flag to determine if the flag is set or not. If the result of the bitwise AND operation is non-zero, it means the flag is present.
In this case, we check if the user has read, written, and execute permissions by performing bitwise AND operations with the corresponding flags. If a flag is set, we output a message indicating that the user has that specific permission.
Why Enums are used in C++?
Enums are used in C++ programming for several reasons:
1. Enhancing code readability: Enums provide meaningful names to constants, making the code self-explanatory and easier to understand.
2. Type safety: Enums help prevent accidental assignment of incorrect values by restricting the variable to a specific set of named constants.
3. Improved maintainability: By using enums, code becomes more modular and less error-prone when changes or additions are required.
Enums offer clarity, type safety, and improved code organization, contributing to efficient and effective software development practices.
No comments:
Post a Comment