In C++ programming, enum or enumeration is a data type consisting of named values like elements, members, etc., that represent integral constants. It provides a way to define and group integral constants. It also makes the code easy to maintain and less complex. In this tutorial, you will learn about C++ enum in detail.
Why Do You Use Enums?
Enums or enumerations are generally used when you expect the variable to select one value from the possible set of values. It increases the abstraction and enables you to focus more on values rather than worrying about how to store them. It is also helpful for code documentation and readability purposes.
What Is C++ Enum?
Enum, which is also known as enumeration, is a user-defined data type that enables you to create a new data type that has a fixed range of possible values, and the variable can select one value from the set of values. For example, suppose you are the owner of an ice cream shop, and you sell a limited range of ice cream flavors. So you want the customers to select only from that collection of ice creams in your shop. This becomes an enumeration with ice cream as the name of enumeration and different flavors of ice creams as its elements.
Now you will have a look at the definition and declaration of the enum.
Definition and Declaration of Enum
To define enum in C++, you must use the enum keyword along with the elements separated by commas. The basic syntax of enum is:
Here, enum is the keyword that instructs the code, the name of the enum is the name that will be assigned to enum, and Element 1, Element 2, …., Element 5 are the values assigned to the enumeration.
Each of these elements has values starting from 0, like vanilla is 0, chocolate is 1, etc. The default values mentioned can be altered during the declaration of the enum in C++.
To declare an enum variable, write the name of the enumeration along with the enum variable.
Here i is the enum variable.
In the above example, you declared enumeration with elements as the name of players, and one captain is to be selected from these players. You declared the enum variable cap in the int main and assigned Avi to the enum variable. Inside the if statement, if Avi's value is equal to the cap, only then the if block executes.
Inside the block, you must print Avi, but it will print Avi's default value, that is 0.
Now have a look at how you can change the default values of enums.
In the above example, you can see that you assigned values to the elements, i.e. cars. You assigned two elements, i.e. Valkyrie, whose value is 240 to the speed1 enum variable, and Roadster, whose value is 250 to the enum variable speed2.
Now, using the comparison operator inside the if statement we are concluding if speed 2 is greater, then the if block executes that means it’s a win for Roadster. Otherwise, the else block executes, which means it’s a win for Valkyrie.
Now, you will look into C++ enums with flags.
Enums With Flags
Suppose you want to buy a mobile with a large storage capacity and better camera quality.
As you can see, you assigned values 1, 2, 4 to the BatteryCapacity, CameraQuality, and LargeStorage, respectively. You assigned these integer values with a power of 2 to avoid overlapping. This is because you want to use two flags at once, i.e. CameraQuality and LargeStorage. Therefore, the power of 2 with the integer values will not let overlapping occur while using the bitwise operator.
You have set the flags CameraQuality and LargeStorage because you want a mobile with both features.
Output 6 indicates both the flags are used.
Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!
After reading this article on C++ enum, you would have learned why to use enums and what is enum in C++, and their definition and declaration. You also learned Enums with flags and how you can change the default values of the elements.
C++ is one of the top programming languages to learn. If you are looking for a more comprehensive learning experience that is designed to help you instance scale up and grown as Full stack software development professional, our Post Graduate Program in Full Stack Development is what you should enroll for. It has all the right ingredients you need to build a full stack career the right way.
On the other hand, if you have any questions on this article on C++ enum, do not hesitate to share them by writing to us in the comments section. We’ll get our SMEs to review and resolve your queries soon. To get a deeper understanding of C++ enum, you could also click on the following link: C++ enum and explore the tutorial video.