C++ Enumeration
In this tutorial, we will learn to work with enumeration (enum). Also, you will learn where enums are commonly used in C++ programming with the help of examples.
Enumeration
- Enumerations are a user-defined data types which can be assigned some limited values.
- In this, you can specify a set of values for a variable and the variable can select only one of the set. It has a fixed set of constants.
- To define an enumeration, keyword 'enum' is used.
- These values are defined by the programmer at the time of declaring the enumerated type.
- Enumerations can be used to define days of a week, months, weathers, etc. The enum constants are static and final implicitly.
Defining an Enum
An enum is defined in the same way as structure with the keyword struct replaced by the keyword enum and the elements seperated by 'comma' as follows
Syntax
enum enum_name{ element1, element2, element3, element4,};
Now let's define an enum of the above example of seasons.
enum season { winter, autumn,spring, summer};
Here, the name of the enumeration is season.
And,winter, spring, autumn, and summer are values of type season.
By default, winter is 0, spring is 1 and so on. You can change the default value of an enum element during declaration (if necessary).
enum season { winter = 0, spring = 4, autumn = 8, summer = 12, };
Enumerated Type Declaration
When you create an enumerated type, only blueprint for the variable is created. Here's how you can create variables of enum type.
enum season { winter, autumn,spring, summer};
int main(){
enum Seasons s;
return 0;
}
Here s is the variable of the enum named Season. This variable will represent a season.
We can also declare an enum variable as follows:
enum season { winter, autumn,spring, summer} s;
Another Example:
enum boolean { false, true };
int main(){
enum boolean check;
return 0;
}
Here, a variable check of type enum boolean
is created.
Here is another way to declare same check variable using different syntax.
enum boolean { false, true } check;
Example 1: C++ Program to find direction using Enumeration
#include <iostream>
using namespace std;
enum direction { North, South, East, West };
int main(){
direction dir;
dir = East;
cout << "Direction: " << dir;
return 0;
}
Output
Day 4
Example 2: C++ Program to find week days using Enumeration
#include <iostream>
using namespace std;
enum week { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
int main(){
week today;
today = Monday;
cout << "Day " << today+1;
return 0;
}
Output
Day 2
Example 3: C++ Changing Default Value of Enums
To print the content of a void pointer, we use the static_cast
operator. It converts the pointer from void*
type to the respective data type of the address the pointer is storing:
#include <iostream>
using namespace std;
enum seasons { spring = 12, summer = 13, autumn = 14, winter = 11};
int main() {
seasons s;
s = Autumn;
cout << "Summer = " << s << endl;
return 0;
}
Output
Autumn = 14
Why enums are used in C++ programming?
Enums are used only when we expect the variable to have one of the possible set of values.
For example:-
#include <iostream>
using namespace std;
enum direction { East = 5, West = 10, North = 15, South = 20 } dir;
int main() {
dir = North;
cout << "Size of enum variable " << sizeof(North) << " bytes.";
return 0;
}
Output
Size of enum variable 4 bytes.
It's because the size of an integer is 4 bytes. This makes enum a good choice to work with flags.
You can accomplish the same task using C++ structures. However, working with enums gives you efficiency along with flexibility.
How to use enums for flags?
Let understand with an example:
enum designFlags { UNDERLINE = 1, ITALICS = 2 BOLD = 4 } button;
Suppose you are designing a button for Windows application. You can set flags UNDERLINE, ITALICS and BOLD to work with text.
There is a reason why all the integral constants are power of 2 in above pseudocode.
// In binary UNDERLINE = 00000001 ITALICS = 00000010 BOLD = 00000100
Since, the integral constants are power of 2, you can combine two or more flags at once without overlapping using bitwise OR | operator. This allows you to choose two or more flags at once.
For example:-
#include <iostream>
using namespace std;
enum printFlags { BOLD = 1, ITALICS = 2, UNDERLINE = 4};
int main() {
int myFlag = UNDERLINE | BOLD ;
// 00000001
// | 00000100
// ___________
// 00000101
cout << "Displaying myFlag = " << myFlag;
return 0;
}
Output
Displaying myFlag = 5
When the output is 5, you always know that underline and bold is used.
Also, you can add flag to your requirements.
if (myFlag & ITALICS) { // statement for italics }
Here, we have added italics to our design. Note, only code for italics is written inside the if statement. You can accomplish almost anything in C++ programming without using enumerations. However, they can be pretty handy in certain situations. That's what differentiates good programmers from great programmers.
Advantages of Enumeration
Some of the advantages of enum are:
1. Enum can be used in switch case.
2. Enum improves type safety.
3. Enum can be traversed.
4. Enum can have fields, constructors and methods.
5. Enum may implement many interfaces but cannot extend any class because it internally extends Enum class.
Next Tutorial
We hope that this tutorial helped you develop better understanding of the concept of Enumeration in C++.
Keep Learning : )
In the next tutorial, you'll learn about C++ Inheritance
.