DEV Community

Cover image for Programming in C++ (Part 9 OOP)
Mark Mahoney
Mark Mahoney

Posted on

Programming in C++ (Part 9 OOP)

This post discusses object-oriented programming using classes in C++. A class is like a struct except that in addition to collecting data it also collects methods that work on that data. This is called encapsulation. I also discuss inheritance and polymorphism that make it easier to reuse code.

Call to Action

Now that you have reviewed the guided code walk-throughs I ask that you write a few programs:

Problem 1

Problem 1 asks you to create a Date class to represent a date.

There should be an int for the day number, month number, and year number declared in the private section. Instead of having a setter for each of those have one method called setDate(int m, int d, int y) that sets the date. You can have a getter method for each piece of data.

Add two constructors- one that takes no data and sets the date to 1/1/2000 and another that takes three ints to set the date.

Include a print() method that will print the date in this format: MM/DD/YYYY and a method called printLong() that prints in this format: MonthName Day, Year. For example today's date would print November 16, 2021.

Include a method to add some number of days, months, and years to a Date:

void addDays(int d)
void addMonths(int m)
void addYears(int y)
Enter fullscreen mode Exit fullscreen mode

Problem 2

Problem 2 asks you to create some related classes for playing card games. The data needed to be stored for a card is a numeric value and a suit. A card is responsible for being able to display itself. For example, when we want to display a two of hearts, ten of diamonds, jack of clubs, or ace of spades the output would look like this:

2 of Hearts
10 of Diamonds
J of Clubs
A of Spades
Enter fullscreen mode Exit fullscreen mode

Next, create a deck class. A deck is a collection of fifty-two cards. Each card is unique. There should be a card with a numeric value from two to the ace in each of the four suits. The responsibilities of the deck class are that it must be able to shuffle itself and deal out some number of cards from the deck. To shuffle the deck you will need to randomly move cards around in the deck. One can generate a random number in C++ using the rand() function.

When dealing out cards the user will ask the deck for some number of cards. If there are enough cards in the deck it should deal those cards out. Once a card is dealt from the deck it cannot be dealt again from the same deck. The user will pass in a vector of cards and the deck will fill it with the number of cards requested. If there aren't enough cards in the deck print an error message and then kill the program with an exit(0).

When creating a class one always has to think about the data needed for the class and the responsibilities of the class. The data for the class should be private and an interface to the class should be provided. Think about how each card and deck should be constructed. Write at least one constructor for each class. Write the card class first and test it in a driver program. Then work on the deck class. To test the deck, create a deck object, ask for 52 cards, and print each of those cards to the screen.

Next, I want you to alter the program so that it will allow a person to evaluate the probabilities of getting certain poker hands. Poker is a card game played with a deck of 52 cards. In this program I only care about the five card variety of poker. The program will repeatedly get groups of five cards from the deck and count how many times each hand occurs.

In most variations of poker the precedence of hands goes like this:

Royal Flush- 5 cards that are a straight (5 cards in numeric order) and a flush (5 cards that are the same suit) from the 10 to the Ace.
Straight Flush- 5 cards that are a straight (5 cards in numeric order) and a flush (5 cards that are the same suit).
Four of a Kind- any 4 cards with the same number.
Full House- three of a kind (3 cards with the same number) and a pair (two cards with the same number).
Flush- any 5 cards of the same suit.
Straight- any 5 cards in numeric order.
Three of a Kind- any 3 cards with the same number.
Two Pair- 2 sets of pairs.
Pair- any 2 cards with the same number.
High Card in your Hand- if you don't have any of the above, your high card is the best hand.
Enter fullscreen mode Exit fullscreen mode

You will need to create additional classes with more responsibilities than the Deck and Card classes.

Your program needs an 'evaluator' that can look at a collection of cards and determine the best hand that can be made from those cards. In order to determine probabilities I would like you to deal a great number of hands and keep track of how many times each hand shows up. In other words, your program might create 100,000 collections of five cards to be evaluated. The evaluator will count how many times a royal flush comes up, how many times a straight flush comes up, an so on. Your program will show the probabilities as percentages of the likelihood of getting each hand.

Below is a driver to illustrate how to use the evaluator:

int main()
    //five card evaluator
    //create a poker evaluator for 5 card poker
    PokerEvaluator fiveCardPokerEvaluator;

    //set the number of hands to play- one hundred thousand this time

    //play all the hands and track the statistics, then print the results to the screen

    return 0;
Enter fullscreen mode Exit fullscreen mode

Comments and Feedback

You can find all of these code playbacks in my free 'book', An Animated Introduction to Programming in C++. I am always looking for feedback so please feel free to comment here or to send me a message. You can follow me on twitter @markm208.

Discussion (0)