Presentation is loading. Please wait.

Presentation is loading. Please wait.

Enumerations CS 2310 - Fall 1999 Enumerations.

Similar presentations


Presentation on theme: "Enumerations CS 2310 - Fall 1999 Enumerations."— Presentation transcript:

1 Enumerations CS Fall 1999 Enumerations

2 C++ Simple Data Types Simple types Integral Floating
char short int long enum float double long double unsigned

3 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 CS Fall 1999 Enumerations

4 Enumeration Types C++ Syntax Examples:
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}; CS Fall 1999 Enumerations

5 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}; CS Fall 1999 Enumerations

6 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}; CS Fall 1999 Enumerations

7 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}; CS Fall 1999 Enumerations

8 More Example Enumerator values can be characters.
enum EscapeChar {Backspace='\b', Bell='\a', Newline='\n', Return='\r'}; CS Fall 1999 Enumerations

9 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 CS Fall 1999 Enumerations

10 Incrementation Weekday = Weekday + 1; // invalid
Weekday = day(Weekday + 1); //valid for (Days = Sunday; Days <= Saturday; Days = day(Days + 1)) { } CS Fall 1999 Enumerations

11 Comparison Weekday <= Thursday; Switch (Weekday)
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:  ; } CS Fall 1999 Enumerations

12 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; } CS Fall 1999 Enumerations

13 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 CS Fall 1999 Enumerations

14 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; CS Fall 1999 Enumerations


Download ppt "Enumerations CS 2310 - Fall 1999 Enumerations."

Similar presentations


Ads by Google