Enumerated Data Types - PowerPoint PPT Presentation

1 / 5
About This Presentation
Title:

Enumerated Data Types

Description:

Enumerated Data Types. representing series of values ... mistakenly use wrong number for a color. updating is also difficult. Use names instead of numbers ... – PowerPoint PPT presentation

Number of Views:231
Avg rating:3.0/5.0
Slides: 6
Provided by: amandaso
Category:

less

Transcript and Presenter's Notes

Title: Enumerated Data Types


1
Enumerated Data Types
2
Enumerated Data Types
  • representing series of values
  • E.g. Simulate a traffic light system
  • Require three values to represent red, yellow,
    and green
  • No built-in C color data type

?
?
?
3
Solution 1
  • Use integer values to represent colors
  • E.g. red 0, yellow 1, and green 2
  • Source code is not readable
  • For example,
  • x 1
  • What does this do assign the number one or the
    color yellow to x?
  • Error prone
  • mistakenly use wrong number for a color
  • updating is also difficult

4
Solution 2
  • Use names instead of numbers
  • Create named constants for each of the values
  • const int RED 0
  • const int YELLOW 1
  • const int GREEN 2
  • Easy to distinguish b/w integers and colors
  • int y 1 // assigns the integer one
  • int x YELLOW // assigns yellow
  • Still x is declared as int

5
Enum provides a solution
  • C uses the enum statement
  • to assign sequential integer values to names
  • provide a type name for declaration
  • E.g.
  • Enum TraficLightColor Red, Yellow, Green
  • int y
  • TraficlightColor x
  • y 1
  • x Yellow

0
1
2
Write a Comment
User Comments (0)
About PowerShow.com