1 / 14

Enumerations

Enumerations. C++ Simple Data Types. Simple types. Floating. Integral. char short int long enum float double long double. unsigned. Short Comings.

marcy
Download Presentation

Enumerations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Enumerations Enumerations

  2. C++ Simple Data Types Simple types Floating Integral 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 Enumerations

  4. 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

  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}; 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}; 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}; Enumerations

  8. More Example Enumerator values can be characters. enum EscapeChar {Backspace='\b', Bell='\a', Newline='\n', Return='\r'}; 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 Enumerations

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

  11. 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

  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; } 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 } 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; } Enumerations

More Related