Enumerations
Download
1 / 14

Enumerations - PowerPoint PPT Presentation


  • 82 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Enumerations' - marcy


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Enumerations

Enumerations

Enumerations


C simple data types
C++ Simple Data Types

Simple types

Floating

Integral

char short int long enum float double long double

unsigned


Short comings
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 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
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
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
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 example1
More Example

Enumerator values can be characters.

enum EscapeChar {Backspace='\b', Bell='\a', Newline='\n', Return='\r'};

Enumerations


Operations on 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
Incrementation

Weekday = Weekday + 1; // invalid

Weekday = day(Weekday + 1); //valid

for (Days = Sunday; Days <= Saturday;

Days = day(Days + 1))

{

}

Enumerations


Comparison
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
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
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
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