320 likes | 750 Views
Enumerations. C++ Simple Data Types. Simple types. Floating. Integral. char short int long enum float double long double. unsigned. Short Comings.
E N D
Enumerations Enumerations
C++ Simple Data Types Simple types Floating Integral char short int long enum float double long double unsigned
Short Comings • In many programming situations, the primitive data types int, char, and float are inadequate to provide the desired degree of program readability. • In some situations, the variables can only take values from a finite set of possibilities. • For examples: Days of the week, Months of the year, College classification, Colors Enumerations
Enumeration Types Enumeration type is a type with a list of descriptive values. C++ Syntax enume numeration-type {enumerator-list}; Examples: enum Seasons {Winter, Spring, Summer, Fall}; enum Color {red, white, green, bule}; enum Days {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturdays}; Enumerations
Value of Enumerators Enumeration type associates names (enumerator) with integer values. By default, the first enumerator has the value of 0 and the value increases by 1 for the next enumerator. Therefore, example 1 can be defined as enum Seasons {Winter=0, Spring=1,Summer=2, Fall=3}; Enumerations
Redefined Values of Enumerators The values of enumerators can be redefined in the same manner as constant variables. In example 3, Sunday can be redefined to be 1 instead of 0. enum day {Sunday=1, Monday, Tuesday, Wednesday, Thursday, Friday,Saturdays}; Enumerations
More Example The values of enumerators can be defined in manner as required. enum MonthLength {Jan=31, Feb=28, Mar=31, Apr=30, May, June=30, July, Aug=31, Sep=Feb+2, Oct, Nov=30, Dec}; Enumerations
More Example Enumerator values can be characters. enum EscapeChar {Backspace='\b', Bell='\a', Newline='\n', Return='\r'}; Enumerations
Operations on Enumerations Assignment enum day {Sunday=1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturdays}; day Days, Weekday, Weekend; int SomeInt; Weekday = Thursday; // valid Weekend = Sunday; // valid Weekday = 5; // invalid SomeInt = Thursday; // valid Enumerations
Incrementation Weekday = Weekday + 1; // invalid Weekday = day(Weekday + 1); //valid for (Days = Sunday; Days <= Saturday; Days = day(Days + 1)) { } Enumerations
Comparison Weekday <= Thursday; This statement is true if the value of Weekday is either Sunday, Monday, Tuesday, Wednesday, or Thursday. Switch (Weekday) { case Monday: ; case Tuesday: ; case Wednesday: ; case Thursday: ; case Friday: ; } Enumerations
Input / Output Stream I/O is defined only for the primitive data types not for enumeration types. Switch (Weekday) { case Monday: cout << "Monday"; break; case Tuesday: cout << "Tuesday"; break; case Wednesday: cout << "Wednesday"; break; case Thursday: cout << "Thursday"; break; case Friday: cout << "Friday"; break; } Enumerations
Function Return Enumeration Type enum SchoolType {PRE_SCHOOL, ELEM_SCHOOL, MIDDLE_SCHOOL, HIGH_SCHOOL, COLLEGE } ; . . . SchoolType GetSchoolData ( void ) // Obtains information from keyboard to determine school level // Postcondition: Function value == personal school level { SchoolType SchoolLevel; int Age; int LastGrade; cout <<“Enter Age : ";// prompt for information cin >> Age; if ( Age < 6 ) SchoolLevel = PRE_SCHOOL; else { cout << “Enter last grade completed in school: “; cin >> LastGrade; if ( LastGrade < 5 ) SchoolLevel = ELEM_SCHOOL; else if ( LastGrade < 8 ) SchoolLevel = MIDDLE_SCHOOL; else if ( LastGrade < 12 ) SchoolLevel = HIGH_SCHOOL; else SchoolLevel = COLLEGE; } return SchoolLevel; // return enum type value } Enumerations
Overloading Output Operator ostream& operator<< (ostream& Out, Day DayVal) { const char ErrorMsg[] = "*** invalid Day value ***"; switch (DayVal) { case Sunday: Out << "Sunday"; break; case Monday: Out << "Monday"; break; case Tuesday: Out << "Tuesday"; break; case Wednesday: Out << "Wednesday"; break; case Thursday: Out << "Thursday"; break; case Friday: Out << "Friday"; break; case Saturday: Out << "Saturday"; break; default: cerr << ErrorMsg; } return Out; } Enumerations