5 Creating and using objects and arrays

This chapter covers

  • Writing a class or structure
  • Scoped enums
  • Using an array instead of a vector when we know how many elements we need
  • Writing a comparison operator
  • Defaulted functions
  • Using std::variant

In this chapter, we will create a deck of cards and write a higher-or-lower card game for guessing whether the next card from a deck is higher or lower. We will create a class for a card and store a deck of cards in an array. We need to consider how to define comparison operators for our cards, as well as how to write constructors and other member functions. We’ll need to use a random shuffle too. We will then extend the game to include jokers and learn how to use std::variant. By the end of the chapter, ...

Get Learn C++ by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.