Jan 17, 2015

ENUM - Make u r names as Data types


Keywords – II
 

Enum :
         An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. 
 
The syntax is :

          enum Tag_name {Variables_names}

For example:
  enum tag_name{ value1, value2,...,valueN };
            
                  Here, tag_name is the name of enumerated data type. And value1, value2,....,valueN are values of type tag_name.
  • If the first enumerator does not have an initializer (if it is not initialized by the user), the associated value is zero. For any other enumerator whose definition does not have an initializer, the associated value is the value of the previous enumerator plus one.
  • Enum variables are the same size as an int variable.This is because each enumerator is automatically
     assigned an integer value based on it’s position in the enumeration list
           In the above exapmle, By default, value1 will be equal to 0, value2 will be 1 and so on... But, the programmer can change the default values to their own values.




 By creating the enum months, you have also created a new data type called months.
 You can use this new data type as you might any other data type.   
For Example:
       months jan ;
    jan = january;

  • At the declaration time, we can also initialize the values with the primitive operators(only a few).

G = -8;
  • The enum constants can be assigned to other enumerators.

  • Already declared enumerators can't be reassigned explicitly.
 enum color { red = 0,yellow, green, blue };
      color red = 11;            // compilation error 
                  However, the compiler will not implicitly cast an integer to an enumeratedvalue. The following will  
produce a  compiler error:
             enum color { red = 0, yellow, green, blue };
  color red1 = 11;           // compilation  error
  • But  there is one way to change enumerators values. This can be done using  
    the static_cast operator by forcing the compiler to put the value in the  
    enumerated data types.
  • Assigning between enumerators can be done but between integers and
    enumerators results in compilation error. And enum also should be of the 
    same type.
  

  • Enumerated types are incredibly useful for code documentation and  readability purposes when 
    you need to represent a specific number of states.

              This will be much easier to read than returning the number values.
Enumerators with class :
                      In gerenal, Enumerators are implicitly converted  into the int data type. It is also possible to
 have char, float etc, thuspreserving data type safety. They are declared with enum class (or enum struct)
 instead of just enum.
For example  :
                      enum class biodata{ name, age, place, dob };
  • Each enum value is scoped with the name of the enum class. In other words,to access the enum
     values, you must write:
    

  Enumerated types declared with enum class also have more control over their underlying 
type. Itmay be any integral data type, such as charshort or unsigned int, which essentially serves 
to determine the size of the type. This is specified by a colon and the underlying type following the 
enumerated type. 
For example:

         enum class biodata : int { dob, age};
         enum class biodata : char{ name, place};

Try urself :

1.



2.
 
 
Filed under  |