C++ Enum class
C++ Enum class
Enum or enumeration is a data type used in C++ programming that consists of named values that represent integral constants, such as elements, members, etc. It offers a means of defining and classifying integral constants.
Additionally, it makes the code simpler and easier to maintain. You will study in-depth information about C++ enum in this lesson.
The user-defined data type enum, commonly referred to as enumeration, allows you to establish a new data type with a fixed range of potential values, and the variable can choose one value from the set of values.
Assume, for instance, that you are the proprietor of an ice cream parlor with a small selection of ice cream flavors. Therefore, you only want the clients to choose ice creams from that selection in your business. This turns into an enumeration with ice cream as the name of the enumeration and various ice cream flavors as its components.
Use of ENUM:
When you want the variable to choose one value out of a potential set, you usually use enums or enumerations.
As a result, you may concentrate more on values rather than how to store them, increasing abstraction. Additionally, it aids readability and description of the code.
To declare an enum class, you use the following syntax:
enum class EnumName;
Here, EnumName is the name you give to the enumeration type.
After declaring the enum class, you can define its values.
To define the values, you use the following syntax:
enum class EnumName {
Value1,
Value2,
Value3,
// ...
};
Here is a list of each significant element:
The keyword "enum class" indicates that you are defining an enumeration with scoped values.
The name you give the enumeration type is called the enumName.
These are the named values or constants in the enumeration: Value1, Value2, Value3, etc.
The fact that the enumeration values are highly typed and do not implicitly convert to integers is one of the main benefits of utilizing an enum class.
An enum class's values each have their own unique type, preventing inadvertent misuse and fostering type safety. When dealing with an enum's values, the enum type must be specified explicitly.
Here's an example that demonstrates the declaration and definition of an enum class:
#include<iostream>
enum class Status; // Declaration
enum class Status {
OK,
Error,
Warning,
// ...
}; // Definition
int main(){
Status currentStatus = Status::OK;
if(currentStatus == Status::OK)
{
std::cout << "Everything is fine." << std::endl;
}
else if (currentStatus == Status::Error)
{
std::cout << "An error occurred." << std::endl;
}
else if (currentStatus == Status::Warning)
{
std::cout << "Warning: Something needs attention." << std::endl;
}
return 0;
}
Output:
![C++ Enum class](https://static.tutorialandexample.com/cpp/cpp-enum-class1.png)
In this example, we first declare an enum class called Status and then define its values: OK, Error, and Warning.
Within the main function, we declare a variable currentStatus of type Status and initialize it with the value Status::OK. We then use if statements to check the value of currentStatus and display an appropriate message based on its value.
Note that you can also assign specific integral values to the enumeration constants. For example:
enum class Numbers {
One = 1,
Two = 2,
Three = 3,
// ...
};
Here's an example that demonstrates the usage of enum class:
#include<iostream>
enum class Day {
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
};
int main(){
Day today = Day::Tuesday;
if(today==Day::Saturday || today==Day::Sunday){
std::cout << "It's the weekend!" << std::endl;
}
else {
std::cout << "It's a weekday." << std::endl;
}
return 0;
}
Output:
![C++ Enum class](https://static.tutorialandexample.com/cpp/cpp-enum-class2.png)
In this example, we define an enum class called Day with the days of the week as its values. We then declare a variable today of type Day and assign it the value Day::Tuesday. By using the Day:: prefix, we explicitly specify the enum type.
The program checks whether today represents a weekend day (Saturday or Sunday) and displays an appropriate message accordingly.
We make the code more clear, self-documenting, and error-free by utilizing the enum class. Additionally, because the enumeration values are contained within the enum class and cannot conflict with those of other enumerations or variables in the same scope, it offers better scoping.
Conclusion:
In conclusion, the C++ enum class is a strong feature that improves the usability and security of enumerations.
Clarity of the code is increased, along with type safety and scoping. When working with an enum's values, it is best to explicitly declare the enum type to create more reliable and manageable code.