A card is a playing-card entity defined by a suit and face value. It can be stored in a deck, displayed, compared, and used in a higher-or-lower game. section 5.0 section 5.1
A simple card uses a Suit scoped enum and a value. A safer design replaces the raw integer with FaceValue, whose explicit constructor rejects values outside 1 through 13. section 5.1.1 section 5.1.2
Listing 5.10 A card class
class Card
{
public:
Card(FaceValue value, Suit suit): #1
value_(value), #2
suit_(suit) #2
{
}
FaceValue value() const { return value_; } #3
Suit suit() const { return suit_; } #3
private:
FaceValue value_; #4
Suit suit_; #4
};
Use the class definition to see how constructors, private members, getters, and default initialization keep cards valid and controlled. section 5.1.3
A practical Card type gains useful behavior incrementally:
- Constructors initialize the face value and suit, while private members prevent arbitrary changes. section 5.1.3
- An overloaded stream-insertion operator prints cards such as “Ace of Hearts”; to_string overloads improve suit and face-value display. section 5.1.4
- A defaulted C++20 three-way comparison uses the card fields in declaration order and supplies the ordinary comparison operators. section 5.1.7
- A shuffled array of cards supports a higher-or-lower game that compares adjacent cards until a guess fails or the deck ends. section 5.2.2
Higher-or-lower play
The game displays the current card, accepts h or l, checks the next card with comparisons, reports an incorrect next card, and counts correct guesses. section 5.2.2
Cards commonly live in a playing_cards.h header and playing_cards.cpp source file inside a cards namespace. Header guards or #pragma once prevent repeated definitions. section 5.1



