204111: Computer and 204111 : Computer and Programming Programming Programming Programming : Enumeration Type Enumeration Type yp yp Week 11 Week 11: Monchai Sopitkamol, Ph.D. Monchai Sopitkamol, Ph.D. Enumeration Type Enumeration Type Enumeration Type Enumeration Type � Motivations Motivations � Declaring an Enumeration Type � Declaring an Enumeration Type Declaring an Enumeration Type Declaring an Enumeration Type � Choosing Enumeration Literal Values Choosing Enumeration Literal Values � Choosing an Enumeration's Underlying Choosing an Enumeration's Underlying Type Type Type Type � Using an Enumeration Using an Enumeration
Motivations Motivations Motivations Motivations � Representing some group of symbolic Representing some group of symbolic constants thru integer variables are not constants thru integer variables are not g very intuitive. very intuitive. � E g � E.g., representing colors (red, green, blue) E g E.g., representing colors (red, green, blue) representing colors (red green blue) representing colors (red green blue) with with 0 0, , 1 1, and , and 2 2, respectively. , respectively. � E.g., representing months (Jan, Feb, Mar, E.g., representing months (Jan, Feb, Mar, …, Dec) with …, Dec) with 0, 1, 2, …, …, Dec) with …, Dec) with 0, , 1, , 2, …, , …, 11 , …, 11 11, respectively. 11, respectively. , respectively. , respectively. Enumeration Type Enumeration Type Enumeration Type Enumeration Type � Motivations Motivations � Declaring an Enumeration Type � Declaring an Enumeration Type Declaring an Enumeration Type Declaring an Enumeration Type � Choosing Enumeration Literal Values Choosing Enumeration Literal Values � Choosing an Enumeration's Underlying Choosing an Enumeration's Underlying Type Type Type Type � Using an Enumeration Using an Enumeration
Declaring an Enumeration Type Declaring an Enumeration Type Declaring an Enumeration Type Declaring an Enumeration Type enum enum enum enum-type type { literal literal-value value1 , , literal literal-value value2 , … } , … } E.g., enum Color { Red, Green, Blue } enum Color { Red, Green, Blue } E.g., or enum Month { Jan Feb Mar or enum Month { Jan, Feb, Mar, …, Dec } enum Month { Jan Feb Mar enum Month { Jan, Feb, Mar, …, Dec } Dec } Dec } or or The symbolic names (literal values) must The symbolic names (literal values) must appear between a pair of braces ({ } ) in a appear between a pair of braces ({ } ) in a appear between a pair of braces ({ } ) in a appear between a pair of braces ({ } ) in a comma- comma -separated list. These names separated list. These names enumerate the values of a new type called enumerate the values of a new type called enumerate the values of a new type called enumerate the values of a new type called Color Color or or Month Month that is an enumeration type. that is an enumeration type. Enumeration Type Enumeration Type Enumeration Type Enumeration Type � Motivations Motivations � Declaring an Enumeration Type � Declaring an Enumeration Type Declaring an Enumeration Type Declaring an Enumeration Type � Choosing Enumeration Literal Values Choosing Enumeration Literal Values � Choosing an Enumeration's Underlying Choosing an Enumeration's Underlying Type Type Type Type � Using an Enumeration Using an Enumeration
Choosing Enumeration Literal Values Choosing Enumeration Literal Values Choosing Enumeration Literal Values Choosing Enumeration Literal Values � Each enumeration has a set of values associated with Each enumeration has a set of values associated with Each enumeration has a set of values associated with Each enumeration has a set of values associated with each element. each element. � By default, the numbering starts at By default, the numbering starts at 0 y y , , g g 0 for the first for the first element and goes up in steps of element and goes up in steps of 1 1. . � You can associate a specific integer constant (such as You can associate a specific integer constant (such as 1 1) ) with an enumeration literal (such as Red), as in the with an enumeration literal (such as Red) as in the with an enumeration literal (such as Red), as in the with an enumeration literal (such as Red) as in the following example: following example: enum Color { Red = enum Color { Red = 1 1, Green, Blue } , Green, Blue } � I MPORTANT: I MPORTANT: The integer value that you initialize an I MPORTANT: I MPORTANT: The integer value that you initialize an The integer value that you initialize an The integer value that you initialize an enumeration literal with must be a compile enumeration literal with must be a compile- -time constant time constant value (such as value (such as 1 1). That is, it must be a constant whose ). That is, it must be a constant whose value does not depend on any run value does not depend on any run- l l d d t d t d d d -time behavior (such ti time behavior (such ti b h b h i i ( ( h h as a method call). as a method call). Choosing Enumeration Literal Values Choosing Enumeration Literal Values Choosing Enumeration Literal Values Choosing Enumeration Literal Values � If you don't explicitly give an enumeration literal If If If you don't explicitly give an enumeration literal d d 't 't li itl li itl i i ti ti lit lit l l a constant integer value, the compiler gives it a a constant integer value, the compiler gives it a value that is one more than the value of the value that is one more than the value of the value that is one more than the value of the value that is one more than the value of the previous enumeration literal, except for the very previous enumeration literal, except for the very first enumeration literal, to which the compiler first enumeration literal, to which the compiler gives a value of 0. gives a value of gives a value of 0 gives a value of . � E.g., in the previous version of E.g., in the previous version of Color Color , the , the underlying values of Red Green and Blue are underlying values of Red Green and Blue are 1 underlying values of Red, Green, and Blue are underlying values of Red, Green, and Blue are 1, , 2, and , and 3 3. . By default, the declaration “ enum Color { Red, By default, the declaration enum Color { Red, enum Color { Red, enum Color { Red, � By default, the declaration “ By default, the declaration Green, Blue } Green, Blue } ” has the values of Red, Green, has the values of Red, Green, and Blue as and Blue as 0 0, , 1 1, and , and 2 2, respectively. , respectively.
Choosing Enumeration Literal Values Choosing Enumeration Literal Values Choosing Enumeration Literal Values Choosing Enumeration Literal Values � You are allowed to give more than one You are allowed to give more than one enumeration literal the same underlying value. enumeration literal the same underlying value. For example, you could code as follows: For example, you could code as follows: “ enum Color { Red, Green, Blue = Red } enum Color { Red, Green, Blue = Red } ” BUT “ enum Color { Red, Green, Blue = White } enum Color { Red, Green, Blue = White } ” will BUT “ will yield compile yield compile- -time error because time error because White White does not exist does not exist i in the enumeration! in the enumeration! i th th ti ti ! ! ALSO “ enum Color { Red = Blue, Green, Blue } enum Color { Red = Blue, Green, Blue } ” will give ALSO “ ” will give compile compile time error either because of circular definition compile compile-time error either because of circular definition time error either because of circular definition time error either because of circular definition error! error! Enumeration Type Enumeration Type Enumeration Type Enumeration Type � Motivations Motivations � Declaring an Enumeration Type � Declaring an Enumeration Type Declaring an Enumeration Type Declaring an Enumeration Type � Choosing Enumeration Literal Values Choosing Enumeration Literal Values � Choosing an Enumeration's Underlying Choosing an Enumeration's Underlying Type Type Type Type � Using an Enumeration Using an Enumeration
Recommend
More recommend