Enumeration - PowerPoint PPT Presentation

1 / 6
About This Presentation
Title:

Enumeration

Description:

Can define a new simple type by listing (enumerating) the literal values ... enum AnimalType { RODENT, CAT, DOG, BIRD, REPTILE, HORSE, SHEEP}; AnimalType myPet = DOG; ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 7
Provided by: anniegro
Category:

less

Transcript and Presenter's Notes

Title: Enumeration


1
Enumeration
2
Enumerated Data Types
  • Can define a new simple type by listing
    (enumerating) the literal values
  • enum ColorType RED, YELLOW, BLUE
  • typedef int ColorType
  • const int RED 0
  • const int YELLOW 1
  • const int BLUE 2

3
Syntax
  • Enum Declaration
  • enum ltNamegt Enumerator, Enumerator,
  • Assignment
  • Identifier ConstIntExpression
  • enum AnimalType RODENT, CAT, DOG, BIRD,
    REPTILE, HORSE, SHEEP
  • AnimalType myPet DOG
  • AnimalType yourPet RODENT
  • yourPet myPet // Valid
  • yourPet BIRD // Valid
  • yourPet 3 // Invalid

4
Example
  • Why Boolean is not an enumerated type
  • enum Boolean FALSE, TRUE
  • Boolean isGreater
  • float a, b
  • isGreater (a gt b) //Invalid

5
Operations on Enumerated Types
  • I/O
  • is only defined for the basic built-in
    typesint, float, char, not for enumerated types
  • Incrementation
  • myPet // Invalid
  • myPet AnimalType(myPet 1) // Valid
  • Comparison
  • determined by their order in their declared
    enumeration

6
Anonymous Data Types
  • A type that does not have an associated
    identifier
  • enum DOG, CAT, BIRD myPet
  • enum DOG, CAT, BIRD yourPet
  • yourPet myPet // Invalid
Write a Comment
User Comments (0)
About PowerShow.com