Download presentation
Presentation is loading. Please wait.
Published bySharon Burke Modified over 8 years ago
1
Enumerated Types Mr. Dave Clausen La Cañada High School
2
Mr. Dave Clausen2 Enumerated Types Enumerated TypesEnumerated Types –This is a new simple type that is defined by listing (enumerating) the literal values to be used in this type. The literal values must be identifiers, not numbers.The literal values must be identifiers, not numbers. They are separated in the list from each other with commas.They are separated in the list from each other with commas. The list is enclosed in braces, a.k.a. curly brackets { }The list is enclosed in braces, a.k.a. curly brackets { } Each identifier corresponds to an integer value that represents its position in the list (starting with zero)Each identifier corresponds to an integer value that represents its position in the list (starting with zero) Each identifier is really then a symbolic constant, and therefore, our “style” would capitalize each identifier.Each identifier is really then a symbolic constant, and therefore, our “style” would capitalize each identifier.
3
Mr. Dave Clausen3 Different from typedef The typedef statement really creates a “synonym” for an existing typeThe typedef statement really creates a “synonym” for an existing type Enumeration creates a new type distinct from any existing type.Enumeration creates a new type distinct from any existing type. Like typedef, the enum declaration must occur before function declarations.Like typedef, the enum declaration must occur before function declarations.
4
Mr. Dave Clausen4 Program Order with enum CommentsComments Preprocessor Directives (# include…)Preprocessor Directives (# include…) using namespace std;using namespace std; Constant DeclarationsConstant Declarations enum, struct, class (we won’t cover struct or class)enum, struct, class (we won’t cover struct or class) typedef (we won’t cover typedef )typedef (we won’t cover typedef ) function declarations (with their comments before the declaration)function declarations (with their comments before the declaration) int main ( )int main ( )
5
Mr. Dave Clausen5 enum Definitions enum DayType {SUN, MON, TUE, WED, THUR, FRI, SAT}; enum SuitType {DIAMONDS, CLUBS, HEARTS, SPADES}; enum PetType {CAT, DOG, FISH, RABBIT, TURTLE}; Once the enumerated type is defined, you can create variables of these types in the int main functionOnce the enumerated type is defined, you can create variables of these types in the int main function –DayType day; –SuitType suit; –PetType pet;
6
Mr. Dave Clausen6 enum Definitions: What you can’t do: enum StarchType {CORN, RICE, POTATO, BEAN}; enum GrainType {WHEAT, CORN, RYE, BARLEY}; You can’t have two of the same identifier in two different enumerated types in the same program. The scope of each enumerated type must be unique. CORN cannot appear in two lists.You can’t have two of the same identifier in two different enumerated types in the same program. The scope of each enumerated type must be unique. CORN cannot appear in two lists. enum VowelType {‘A’, ‘E’, ‘I’, ‘O’, ‘U’}; enum FinishType {1st, 2nd, 3rd}; These are both illegal because the items are not valid identifiers.These are both illegal because the items are not valid identifiers. –VowelType is using characters (char) You can’t use predefined data typesYou can’t use predefined data types –FinishType identifiers start with a number
7
Mr. Dave Clausen7 Order in Enumerated Types By default, the identifiers specified in an enum declaration are given integer values since enums are ordinal types. (Similar to the way that characters are represented by integers using the ASCII code.)By default, the identifiers specified in an enum declaration are given integer values since enums are ordinal types. (Similar to the way that characters are represented by integers using the ASCII code.) –The first identifier in your declaration is given the value of zero. The identifier you listed second is given the value of one, etc.. –Though seldom necessary, you could assign different values to the identifiers in your list when you define them, for example: enum SummerMonthType {JUNE=6, JULY=7, AUGUST=8};
8
Mr. Dave Clausen8 Operations on Enumerated Types Operations that don’t work:Operations that don’t work: –You cannot input or output enumerated types directly using cin or cout. Given the following: enum WeekDayType {MON, TUE, WED, THUR, FRI}; enum WeekEndDayType {SAT, SUN}; WeekDayType day; day = WED; day = WED; cout<< day; cout<< day; The output for this would be 2 not WED, since only the integer value that represents the position of WED in the list would be assigned to the variable day.
9
Mr. Dave Clausen9 Output of Enumerated Types Switch statements are usually used to output enum identifiers.Switch statements are usually used to output enum identifiers. void Display_Week_Day (WeekDayType week_day) { switch(week_day) { switch(week_day) { case MON : cout<< “Monday”<<endl; case MON : cout<< “Monday”<<endl; break; break; case TUE : cout<< “Tuesday”<<endl; break; break; case WED : cout<< “Wednesday”<<endl; break; break; case THU : cout<< “Thursday”<<endl; break; break; case FRI : cout<< “Friday”<<endl; break; break;}}
10
Mr. Dave Clausen10 Input of Enumerated Types Switch statements are usually used to input enum identifiers.Switch statements are usually used to input enum identifiers. void Enter_Week_Day (char week_day_letter, WeekDayType &week_day) { switch(week_day_letter) { switch(week_day_letter) { case ‘M’ : week_day = MON; case ‘M’ : week_day = MON; break; break; case ‘T’ : week_day = TUE; break; break; case ‘W’ : week_day = WED; break; break; case ‘R’ : week_day = THU; break; break; case ‘F’ : week_day = FRI; break; break;}}
11
Mr. Dave Clausen11 An Operation That Works with Enumerated types: Comparison ComparisonComparison –When you compare two values of enumerated types, the ordering is determined by the order in which the enumerators were placed in their declaration. e.g. enum DayType {SUN, MON, TUE, WED, THUR, FRI, SAT}; //if the variable day contained the value SUN, then the following comparison would be TRUE: day < MON day < MON
12
Mr. Dave Clausen12 Incrementing Enumerated Types To increment an enumerated type, you must use explicit type conversion (type casting).To increment an enumerated type, you must use explicit type conversion (type casting). –The following don’t use a explicit type conversion and therefore, are invalid: –day = day + 1; //INVALID –day++;//INVALID –for(day=SUN; day <=SAT; day++)//INVALID –The correct way to increment enumerated types is to use explicit type conversion: day = DayType(day + 1);day = DayType(day + 1); for(day = SUN; day <= SAT; day = DayType(day + 1))for(day = SUN; day <= SAT; day = DayType(day + 1))
13
Mr. Dave Clausen13 Functions that Return an Enumerated Data Type Functions can return any valid data type, simple or user defined, except that of an array.Functions can return any valid data type, simple or user defined, except that of an array. Therefore, a function can return a data type that is enumerated.Therefore, a function can return a data type that is enumerated. We could have written our Enter_Day function with a return type, rather than a void function. Here’s the revised declaration:We could have written our Enter_Day function with a return type, rather than a void function. Here’s the revised declaration: WeekDayType Enter_Week_Day (char week_day_letter);
14
Mr. Dave Clausen14 Sample Program Using enum BirthdayDev.cpp
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.