Enumerated types work when you know in advance a finite (usually short) list of values that a data type can take on. It is another user defined type which provides a way for attaching names to numbers, thereby increasing comprehensibility of the code. The enum keyword automatically enumerates a list of words by assigning them values 0,1,2,3,4, and so on.
The general form of enum is:
enum variable name { list of constants separated by commas };
where enum is a keyword
variable name is the user defined variable name
list indicates the fixed constant values
eg.,
enum days_of_week {sun, mon, tue, wed, thu, fri, sat};
Once we have specified the days of the week as shown we can define variable of this type.
Example:
# include<iostream.h>
enum days_of_week {sun, mon, tue, wed, thu, fri, sat };
void main( )
{
days_of_week day1, day2,day3;
day1 = mon;
day 2 = fri;
int diff = day2 - day1;
cout<<"days between = "<<diff<<endl;
if (day1<day2)
cout<< "day1 comes before day2\n";
}
the values listed inside braces of enum keyword are called members. Enumerated means that all the values are listed.
0 Comments