O'Reilly logo

C++ Primer Plus, Fourth Edition by Stephen Prata

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Enumerations

The C++ enum facility provides an alternative means to const for creating symbolic constants. It also lets you define new types but in a fairly restricted fashion. The syntax for using enum resembles structure syntax. For example, consider the following statement:

enum spectrum {red, orange, yellow, green, blue, violet, indigo, ultraviolet};

This statement does two things:

  • It makes spectrum the name of a new type; spectrum is termed an enumeration, much as a struct variable is called a structure.

  • It establishes red, orange, yellow, and so on, as symbolic constants for the integer values 0—7. These constants are called enumerators.

By default, enumerators are assigned integer values starting with 0 for the first enumerator, 1 for ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required