Skip to main content

How To Think Like a Computer Scientist C++ Edition The Pretext Interactive Version

Section 13.1 Enumerated types

In the previous chapter I talked about mappings between real-world values like rank and suit, and internal representations like integers and strings. Although we created a mapping between ranks and integers, and between suits and integers, I pointed out that the mapping itself does not appear as part of the program.
Actually, C++ provides a feature called an enumerated type that makes it possible to (1) include a mapping as part of the program, and (2) define the set of values that make up the mapping. For example, here is the definition of the enumerated types Suit and Rank:
enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES };

enum Rank { ACE = 1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE,
TEN, JACK, QUEEN, KING };

Note 13.1.1.

By default, the first value in the enumerated type maps to 0, the second to 1, and so on.
Within the Suit type, the value CLUBS is represented by the integer 0, DIAMONDS is represented by 1, etc.
The definition of Rank overrides the default mapping and specifies that ACE should be represented by the integer 1. The other values follow in the usual way.
Once we have defined these types, we can use them anywhere. For example, the instance variables rank and suit are can be declared with type Rank and Suit:
struct Card {
  Rank rank;
  Suit suit;

  Card(Suit s, Rank r);
};
The types of the parameters for the constructor have changed, too. Now, to create a card, we can use the values from the enumerated type as arguments:
Card card (DIAMONDS, JACK);
By convention, the values in enumerated types have names with all capital letters. This code is much clearer than the alternative using integers:
Card card (1, 11);
Listing 13.1.1. This active code uses the enumerated types created above to construct Card objects. Feel free to modify the values that the cards are being initialized to in the constructor: this will change the output from the print function. Notice how this is much clearer than using integers.
Because we know that the values in the enumerated types are represented as integers, we can use them as indices for a vector. Therefore the old print function will work without modification. We have to make some changes in buildDeck, though:
int index = 0;
for (Suit suit = CLUBS; suit <= SPADES; suit = Suit(suit+1)) {
  for (Rank rank = ACE; rank <= KING; rank = Rank(rank+1)) {
    deck[index].suit = suit;
    deck[index].rank = rank;
    index++;
  }
}
In some ways, using enumerated types makes this code more readable, but there is one complication. Strictly speaking, we are not allowed to do arithmetic with enumerated types, so suit++ is not legal. On the other hand, in the expression suit+1, C++ automatically converts the enumerated type to integer. Then we can take the result and typecast it back to the enumerated type:
suit = Suit(suit+1);
rank = Rank(rank+1);
Actually, there is a better way to do this—we can define the ++ operator for enumerated types—but that is beyond the scope of this book.

Checkpoint 13.1.1.

Multiple Response: What can we do with enumerated types?
  • Perform arithmetic.
  • We are not allowed to do arithmetic with enumerated types.
  • Include a mapping as part of the program.
  • This is the purpose of an enumerated type.
  • Use the same set of values in multiple mappings.
  • Variables in one enumeration type cannot be used in another enumeration type.
  • Define the set of values that make up a mapping.
  • This is the purpose of an enumerated type.
  • Use them as indices for a vector.
  • Since the values in enumerated types are represented as integers, we can use them as vector indices.

Checkpoint 13.1.2.

Assume we have the following struct defined by this enumerated type. What will be printed by the print function?
enum Scoops { SINGLE = 1, DOUBLE, TRIPLE };
enum Flavor { VANILLA, CHOCOLATE, STRAWBERRY, COOKIESNCREAM, MINTCHIP, COOKIEDOUGH };
enum Order { CUP, CAKECONE, SUGARCONE, WAFFLECONE }

struct iceCream {
    Scoops scoops;
    Flavor flavor;
    Order order;

    iceCream (Scoops s, Flavor f, Order o);
    printOrder() {
    // To save space, I didn't include the mapping.
    // I'm sure you can still figure it out.
    cout << "Who ordered a " << scoops[scoop] 
        << " scoop of " << flavors[flavor] 
        << " in a " << orders[order] << ?;
   }
};

int main() {
  iceCream icecream (2, 3, 2);
  iceCream.printOrder();
}
  • Who ordered a triple scoop of Cookies ’n’ Cream in a sugar cone?
  • Remember that we performed an override for one of the enumerated types!
  • Who ordered a double scoop of Strawberry in a cake cone?
  • Remember that the default enumeration starts at 0.
  • Who ordered a double scoop of Cookies ’n’ Cream in a sugar cone?
  • 2 corresponds to "double", 3 corresponds to "Cookies ’n’ Cream", and 2 corresponds to "sugar cone".
  • Who ordered a triple scoop of Strawberry in a cake cone?
  • Remember that we performed an override for one of the enumerated types! The default enumeration starts at 0.
  • Who ordered a triple scoop of Mint Chocolate Chip in a Waffle Cone?
  • Take another look at how we defined our enumerated types.

Checkpoint 13.1.3.

You have attempted 1 of 4 activities on this page.