Enumeration Types and enum in C++

Written by Ali on. Posted in C++

Enumeration in C is a convenience feature added to make coding easy. By use of enumeration, or more appropriately enum, a lot of hassle is simplified and code can be made easier to understand. Enum is a user-defined type used to store set of named constants called as enumerators. enum is a reserved keyword in C . 

Enum is actually a user defined type used to declare a list of named constants that can be used elsewhere in the code, a tool to represent a range of values of things in a given context. An example can be taken as a list of animals or a list of sports or a list of colors that can be used elsewhere in the code to represent some variables to express ease. At times in code, this can ease up the code a lot easier if the variable names are used and assigned appropriately.

Consider the example of a list of sports, than can contain football, basketball, hockey, cricket, badminton, swimming and many more. If you are to select from a list of sports that require a ball to be played, then you can try the following code.

</p>
<p>int main()</p>
<p>{</p>
<p>.  enum sports { football, basketball, hockey, cricket, badminton, swimming };</p>
<p>.  sports myfav = football;</p>
<p>.  cout &lt;&lt; myfav;</p>
<p>.  return 0;</p>
<p>}</p>
<p>

Now the cout result casino online will catch your attention and you’ll definitely be perplexed as what on earth has happened. You have coded an instruction to display the content of ‘myfav’ but an integer is outputted on the console. This is because the enum type ranks its enumerators in the form of numbers that are arranged in the ascending order from the first input that is assigned value ‘0’. The following enumerators are allocated the successive numbers till the last one. This is the reason the value “0” is displayed on the console. However, in order to display the information held by a specific enum type, you have to take pain to write the following code right after assigning the enumerator to your enum type ‘myfav’, remembering the order in which your enumerators are placed.

</p>
<p>switch (myfav)</p>
<p>{</p>
<p>.  case 0:</p>
<p>.  cout &lt;&lt; “Football”;</p>
<p>.  break;</p>
<p>.  case 1:</p>
<p>.   cout &lt;&lt; “Basketball”;</p>
<p>.   break;</p>
<p>.  case 2:</p>
<p>.   cout &lt;&lt; “Hockey”;</p>
<p>.  break;</p>
<p>.  case 3:</p>
<p>.   cout &lt;&lt; “Cricket”;</p>
<p>.   break;</p>
<p>}</p>
<p>

 

Also enum can be used to do operation on them. That is only because the value stored in them is the place value of that enumerator in that enum that helps to evaluate the operation placed. Have a look at the following code.

</p>
<p>int main()</p>
<p>{</p>
<p>enum sports{ football, basketball, hockey, cricket, badminton, swimming };</p>
<p>if(football &lt;= basketball)</p>
<p>cout &lt;&lt; “true’;</p>
<p>else</p>
<p>cout &lt;&lt; “false”;</p>
<p>return 0;</p>
<p>}</p>
<p>

In this code, the aggregate operation ‘<=’ or less than or equal to is being used. This is allowed as the enumerators are allocated default place values by the system that allows it to identify what is placed at specific place values. Only limited aggregate operation are allowed as their usage can only be restricted to relational operations only. It can also be done in the following way.

</p>
<p>int main()</p>
<p>{</p>
<p>enum sports{ football, basketball, hockey, cricket, badminton, swimming };</p>
<p>sports myfav = football;</p>
<p>sports yourfav = hockey;</p>
<p>if(myfav &lt;= yourfav)</p>
<p>cout &lt;&lt; “true’;</p>
<p>else</p>
<p>cout &lt;&lt; “false”;</p>
<p>return 0;</p>
<p>}</p>
<p>

That’s all from us to offer you at enums. Stay safe and keep your eyes sharp for the next hearing from codingmash.

Tags: , , ,

Trackback from your site.

Leave a comment