Enumeration in C++

 Enumeration in C++




An enumeration is a user-defined data type that consists of integral constants. To define an enumeration, keyword enum is used.
enum season { spring, summer, autumn, winter };
Here, the name of the enumeration is season.
And, springsummer and winter are values of type season.
By default, spring is 0, summer is 1 and so on. You can change the default value of an enum element during declaration (if necessary).
enum season 
{   spring = 0, 
    summer = 4, 
    autumn = 8,
    winter = 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 boolean { false, true };

// inside function
enum boolean check;
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: Enumeration Type

  1. #include <iostream>
  2. using namespace std;
  3. enum week { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
  4. int main()
  5. {
  6. week today;
  7. today = Wednesday;
  8. cout << "Day " << today+1;
  9. return 0;
  10. }
Output
Day 4

Example2: Changing Default Value of Enums

  1. #include <iostream>
  2. using namespace std;
  3. enum seasons { spring = 34, summer = 4, autumn = 9, winter = 32};
  4. int main() {
  5. seasons s;
  6. s = summer;
  7. cout << "Summer = " << s << endl;
  8. return 0;
  9. }
Output
Summer = 4

Why enums are used in C++ programming?

An enum variable takes only one value out of many possible values. Example to demonstrate it,
  1. #include <iostream>
  2. using namespace std;
  3. enum suit {
  4. club = 0,
  5. diamonds = 10,
  6. hearts = 20,
  7. spades = 3
  8. } card;
  9. int main()
  10. {
  11. card = club;
  12. cout << "Size of enum variable " << sizeof(card) << " bytes.";
  13. return 0;
  14. }
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 us take an example,
  1. enum designFlags {
  2. ITALICS = 1,
  3. BOLD = 2,
  4. UNDERLINE = 4
  5. } button;
Suppose you are designing a button for Windows application. You can set flags ITALICSBOLD and UNDERLINE to work with text.
There is a reason why all the integral constants are power of 2 in above pseudocode.
// In binary

ITALICS = 00000001
BOLD = 00000010
UNDERLINE = 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,
  1. #include <iostream>
  2. using namespace std;
  3. enum designFlags {
  4. BOLD = 1,
  5. ITALICS = 2,
  6. UNDERLINE = 4
  7. };
  8. int main()
  9. {
  10. int myDesign = BOLD | UNDERLINE;
  11. // 00000001
  12. // | 00000100
  13. // ___________
  14. // 00000101
  15. cout << myDesign;
  16. return 0;
  17. }
Output
5
When the output is 5, you always know that bold and underline is used.
Also, you can add flag to your requirements.
if (myDesign & ITALICS) {
    // code 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

Comments

Popular posts from this blog

C++ Virtual Function

Function Overriding in C++ [With Example]

C++ Inheritance(Why inheritance should be used?)