Updated March 20, 2023
Introduction to Enum in C++
C++ has so many data types and one of the most important ones is an enum. Enum is a user-defined data type that consists of a fixed set of constants or we can say a set of integral constants. The enum keyword is used to define an enumeration in the C++ programming language. It can be used to represent a set of directions and days as enum are implicitly final and static. To make code easy in maintaining and reading enum data type is used to assign names to constants or a fixed set of values which makes code faster and easy to find.
Syntax:
enum enum-name { list of names } var-list ;
enum color { red, blue, orange } c ;
c = red ;
How does Enum Work in C++?
Here we discuss how enumeration works in C++ programming language with the help of code:
Code:
// defining a new enumeration named Animal
enum Animal
{
// each enum will be separated by a comma and we will define all possible value this enum can hold.
Animal_Alpaca ,
Animal_Tiger ,
Animal_Lion ,
Animal_Kangaroo ,
Animal_Zebra ,
Animal_Bear ,
Animal_Deer , // last enum should end with a comma.
} ; // as the enum must end with a semicolon.
// here we are defining a few variables of enumeration type Animal
Animal omnivore = Animal_Tiger ;
Animal type ( Animal_Lion ) ;
Animal origin { Animal_Zebra } ;
As we have defined an enumeration called Animal as it does not allocate any memory only when we define a variable of the enum type such as we define enum variable for Animal as Animal type. Memory is allocated for Animal type at that time only. Also, each enumeration is separated by a comma and after defining all enum it must end with a semicolon. An enumerator name can’t be used in multiple enumerations within the same namespace because enumerators are placed into the same namespace in an enumeration.
Based on the position in the enumeration list each enumerator will be assigned an integer value. Value 0 ( zero) will be assigned to the first enumerator by default. And after that value count will be increased by 1 for next enumerators defined in the list.
enum Animal
{
Animal_Alpaca , // assigned value is 0
Animal_Tiger , // assigned value is 1
Animal_Lion , // assigned value is 2
Animal_Kangaroo , // assigned value is 3
Animal_Zebra , // assigned value is 4
Animal_Bear , // assigned value is 5
Animal_Deer
} ; // assigned value is 6
In case you want to define your own value for enumerators you can do that explicitly and these values can be either positive or negative.
enum Animal {
Animal_Alpaca = -9 , // assigned value is -9
Animal_Tiger , // assigned value is -8
Animal_Lion , // assigned value is -7
Animal_Kangaroo = 5, // assigned value is 5
Animal_Zebra // assigned value is 6
} ;
Examples of enum in C++
It’s time to discuss enum methods in C++ with explained examples:
1. Unscoped Enumeration Method
- enum enum-name { enumerator = value, enumerator = value, ..}
In the above-unscoped enumeration, the underlying type is not fixed.
- enum enum-name : type { enumerator = value, enumerator = value, ..}
In the above-unscoped enumeration, enum is redefined and the underlying type is fixed.
- enum enum-name : type ;
In the above-unscoped enumeration, enum is redefined and the underlying type is type.
C ++ code example:
#include <iostream>
using namespace std;
int main()
{ // enum named Gender is defined here
enum GenderCheck { Male,
Female };
// C Gender type variable are created here
GenderCheck gender = Male;
switch (gender) { // switch for switching to gender
case Male:
cout << "The gender is Male";
break;
case Female:
cout << "The gender is Female";
break;
default:
cout << "The value can either be Male or Female";
}
return 0;
}
Output:
2. Scoped Enumeration Method
- enum struct |class name { enumerator = value, enumerator = value, ..}
In the above, scoped enumeration underlying type is int.
- enum struct |class name: type { enumerator = value, enumerator = value, ..}
In the above, scoped enumeration underlying type is type.
- enum struct |class name ;
In the above, scoped enumeration enum is redefined and the underlying type is int.
- enum struct |class name : type ;
In the above, scoped enumeration enum is redefined and the underlying type is type.
C ++ code example:
#include <iostream>
using namespace std;
enum day { Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday };
int main()
{
int x;
for (x = Monday; x <= Sunday; x++)
cout << x << " ";
return 0;
}
Output:
Rules and Regulation for Enum in C++
Below are a few of the rules and regulation for enum in C++.
- It is recommended to use enum over macros because macros don’t have any data type & can override previous values.
- To represent a set of same-named constants, always use enumerations.
- Instead of using simple “enum” use “enum class” to avoid ambiguity.
- For safety and ease always define operations on enumerations.
- For defining or using enumerators don’t use names in ALL_CAPS.
- Always name enumerations, don’t leave on default.
- If necessary then only specify the underlying type of the enumeration.
- Allocate values to enumerators only when it’s needed.
Advantages of Enum Data Types
- Enum is constant rather than being a number, it helps in increasing the source code readability.
- Enum also forces you to think about all the maximum possible values an enumerator can take in codes.
- Enum always restricts the values an enum variable can take in programming.
Conclusion
Enum is important to use in programming, it saves a lot of time and memory space which indirectly makes the code efficient and faster in terms of performance. Because when you define an enum only blueprint for the variable will be created and that requires no memory space.
Recommended Articles
This is a guide to Enum in C++. Here we discuss the working, rules, advantages, and examples of enumeration in C++ with code implementation. You may also look at the following articles to learn more –