Enumeration A different approach to defining your own

















- Slides: 17

Enumeration A different approach to defining your own data type.

What is Enumeration? • A list of data types defined by the programmer. • Works when you know in advanced the list of variables that you will define. • Needs to be a finite listing; usually a short list. • You can write great code without ever using enumeration.

Days of the Week • A good example of enumeration are days of the week. • Complete list of possible values for a day of the week is (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday). • Lets look at an example: dayenum. cpp

Days of the Week • An enum declaration defines the set of all names that will be permissible values of the type. • Those permissible values are called enumerators. • The Days of the Week example has seven enumerators, each day of the week.

Enumeration • An enumeration is a list of all possible values. • This is unlike an int, which is given in terms of a range of values. • In an enum you must give a specific name to every possible value.

Enumeration • Once you have declared the enum type days_of_week as shown in our example, you can define variables of this type. • In our example, it has two such variables, day 1 and day 2 defined. • days_of_week day 1, day 2;

Enumeration • Variables of an enumerated type, like day 1 and day 2, can be given any of the values listed in the enum declaration. • You can’t use values not defined. • day 1 = New Years; is illegal in our example.

Enumeration • You can use standard arithmetic operators on enum types. • In the program we subtract two values and got a proper answer. • This wouldn’t make sense with every enum type you design.

Enumeration • Let’s take another example of enum. • enum pets {cat, dog, hamster, canary}; • Here it wouldn’t make sense to say: dog + canary or (cat < hamster).

Enumeration • Enumerations are treated internally as integers. • This explains why we can perform arithmetic and relational operations on them. • Usually the first name in the list is given the value of 0.

Enumeration • Don’t think of enum just as a fancy int. • A statement like day 1 = 5; is illegal! • Don’t take advantage of the fact that these are internally stored as integers.

Enumeration Let’s look at a word count program example. • wdcount. cpp

Enumeration • We found out that when you declare an enum, the first enumerator was given the integer value 0, and so on. • If you wanted to start out with a different value you can set this up.

Enumeration • enum days_of_week {Sunday = 1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}; • So now Sunday has the internal value of 1, Monday has the value or 2 and so on. • Actually you can use the equal sign to give a specified value to any enumerator.

Enumeration • Enumeration is not perfect. • What do you think the following code would produce? enum direction {north, south, east, west}; direction dir 1 = south; cout << dir 1;

Enumeration Other Examples • enum switch {on, off}; • enum meridian {am, pm}; • enum coins {penny, nickel, dime, quarter, half-dollar, dollar}; • enum months {jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec};

Chapter 4 review • Structures are important because they have the same syntax of classes. • Structures are grouping data types into new data types. • An enumeration is a programmer defined type limited to a fixed list of values.