Section 3 - A Card Game
1A.3.1 A Class for a Deck of Cards
Do you play Bridge, Hearts, Poker or Black Jack? If so, you've signed up for the right class. Our first example will deal (ahem) with the topic of playing cards.
We will create a class called Card whose objects we will imagine to be representations of playing cards. Each object of the class will be a particular card such as the '3' of clubs or 'A' of spades. The private data of the class is fairly obvious. We will need a card value ('A', '2', '3', ... 'Q', 'K') and a suit (spades, hearts, diamond, clubs).
Before we design this class, let's see a very simple main() (also called the client, since it uses, or employs, our class) that simply declares a few Card objects and displays their values on the screen. We want to do a lot of fun things with our Card objects, but not yet. We start very carefully and slowly.
int main() { Card card1, card2('5'), card3('9', hearts), card4('j', clubs), card5('1', diamonds); if ( ! card1.set(2, clubs) ) cout << "incorrect value (2, clubs) passed to card::set()\n\n"; if ( ! card1.set('2', clubs) ) cout << "incorrect value ('2', clubs) passed to card::set()\n\n"; cout << card1.toString() << endl << card2.toString() << endl << card3.toString() << endl << card4.toString() << endl << card5.toString() << endl<< endl; card1 = card4; cout << "after assigning card4 to card1:\n"; cout << card1.toString() << endl << card4.toString() << endl << endl; return 0; }
We can't run this main() without embedding it into a full program that has all of the Card class definitions, but let's look at a copy of the run anyhow, so we can see what the correct output for this program is:

We can learn a lot about what this class Card must be and do by looking at the main() and the run. Let's make a list of things that we can surmise just by looking at the client.
- We are declaring several Card objects and sending a varying number of arguments to the constructor. For example, look at this line:
Card card1, card2('5'), card3('9', hearts), card4('j', clubs), card5('1', diamonds);We see that card1 is declared with no arguments. This tells us that the class has a constructor that does not require any arguments. Do you remember what a no-parameter constructor is called? Right! A default constructor. So there must be one of those in our class.
However, we also see that card2 is providing the constructor with one argument and card3, card4 and card5 with two arguments . So we must also have 1-parameter and 2-parameter constructors in our Card class.
Card ... card4('j', clubs), ...The first argument, the value, is passed to the constructor as what data type? It is surrounded by single quotes '5', '9', 'j', so it must be a char. But the suit is a little odd. It is a word like clubs or diamonds with no quotes around it. Words without quotes in C++ usually refer to variable names, but in this case, that's not what they are. Here we are using enumerated or enum types. You may not have had enums in your first course so you will learn about them this week.
Evidently, in order to fully understand how to write this class, we are going to have to become familiar with three simple C++ topics:
- Enum types
- Default parameters
- The newer string class and the older CString char arrays.
We will spend the next three sections explaining those topics then return to play some cards.