liveBook · Manning

Suit

As explained in Learn C++ by Example

A suit is one of the four categories that classify playing cards in a standard deck.

A suit classifies a playing card into one of four categories: Hearts, Diamonds, Clubs, or Spades. A card combines a suit with a value from ace through king. section 5.1

Represent the suit with the C++11 scoped enumeration Suit. Write values as Suit::Hearts or Suit::Clubs, avoiding accidental comparisons with unrelated enumerations and requiring an explicit cast when treating a suit as a number. section 5.1.1

Putting a suit on a card

A Card can be aggregate-initialized with a numeric value and a suit: Card card{2, Suit::Clubs}. Stream output can initially show the underlying enum number, then improve to readable text such as “2 of Clubs” by converting the suit with to_string. section 5.1.1 section 5.1.4

Listing 5.4 Scope enum for suits
#pragma once            #1
 
namespace cards         #2
{
    enum class Suit {   #3
        Hearts,
        Diamonds,
        Clubs,
        Spades
    };
}

Inspect the complete scoped enumeration and its four suit enumerators. section 5.1.1

How Learn C++ by Example covers it

How deep it goes
Defines a scoped enum, explains qualification and casts, and demonstrates string conversion for display.
Examples
Uses Hearts, Diamonds, Clubs, Spades, Card{2, Suit::Clubs}, and readable output such as “2 of Clubs.”
What it assumes
Assumes basic C++ structs and initialization while teaching scoped-enum qualification and explicit conversion.
What you can do after
Build it from scratch by declaring Suit, constructing cards with it, and displaying suit names through stream and string functions.
How it connects
Connects suits to playing cards, scoped enumerations, aggregate initialization, stream insertion, and string conversion.

Also explained in

Compare how 2 books explain suit →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn C++ by Example. The section links point to the parts of the book it draws on.