liveBook · Manning

Enum

As explained in Object-Oriented Software Design in C++

A type whose values are restricted to a finite set of named constants.

Use an enumeration when an attribute has a limited set of named values. For book genres, Genre replaces strings with constants such as ADVENTURE, CLASSICS, HORROR, and SCIFI. section 2.3.2

Compared with strings, an enumeration provides:

  • Compiler-detectable misspellings of named constants. section 2.3.2
  • Efficient run-time comparisons between known values. section 2.3.2

Book genres

A book can store Genre::ADVENTURE or Genre::CLASSICS instead of a genre string. A catalogue can then search for Genre::HORROR without handling capitalization or spelling variations.

How Object-Oriented Software Design in C++ covers it

How deep it goes
Defines an enum class, demonstrates its constants, and stops short of broader enumeration design concerns.
Examples
Uses book genres including ADVENTURE, CLASSICS, HORROR, and SCIFI in construction and catalogue-search examples.
What you can do after
Evaluate whether a limited attribute should use an enumeration rather than a string.
How it connects
Connects enumeration with strings, compiler checking, run-time comparison, book attributes, and catalogue searching.

Also explained in

Compare how 23 books explain enum →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Object-Oriented Software Design in C++. The section links point to the parts of the book it draws on.