Pointers, Virtual Functions, Casting

Lets dive deeper into pointers, virtual functions and casting - Page 1

Lets look at using pointers in a class. We need to also look at shallow versus deep copying and the copy constructor and copy assignment operator. Finrally we will look at virtual functions and casting.

Otter dancing with a fish

You will need to install either Visual Studio Community 19 on the PC with C++ drivers or XCode on the mac. You will also need a GitHub account which is free to sign up for as we will be using version control.

1. Project Set Up & Card Class

Lets start by getting a project and up and running in Visual Studio or XCode. Lets set up a Card class and make some improvements over our last version.


Setting up our Dev Environment

Open up Visual Studio 19 Community or xcode. You should see a screen like this. Click on the Create a New Project button. Select a Console App as we will only be working withing the console. Name the solution CPP-Card-Deck and select a location on your hard drive to save it in.


Add Card Class

Add a .cpp and .h class called Card to the project.


Include Types & Namespaces

Lets include iostream, string and std::array class. We will use the std::array and not the cstyle array as we have a fixed known number of cards and don't need a dynamic container. Add a namespace called PlayingCard.


Number & Suit Class Enumerator

Lets add a class and number suit enumerator to the project.


Card Struct

Now we should add a struct to hold the card number and suit. There is no invariate so we can use a struct instead of a class. Add a simple constructor declaration.


Define Struct Constructor

Define Struct Constructor by initialize the CardNumber and CardSuit passed to it. We will start with the single constructor.


Test Struct in main()

Go back to the main() function and create a CardValue struct. Compile and make sure there are no errors.


Add Static Array For Output

Lets add two static std::arrays that hold strings for the card numbers and suits. It can be static as it will not change per instance and the entire class can share the two arrays. We are also going to cast from enum to integer. Our enumerators start at 1 so we will return an error if the user tries to access the 0 entry.


Define Static String Arrays

We will add text for suits and numbers in the exact same order they appear in the enumerators. We will add the text error if it tries to access the 0 enumerator that is undefined for both enumerators.


Suit and Number Getters

Add two functions that return the string for the card numbera nd card suit. To access the array we need to static-cast the enumerator to an integer. Since it is a one line function we can define it in the .h header. Please note the keyword inline. For simple functions, creating them inline can have small performance improvements especially if this function is called often. Since the function


Card Constructor

Add a constructor to card and pass it a reference to a CardNumber and CardSuite to create a new card.


Define Constructor

Intialize the number and suit with the enumerators passed to the class constructor.


Create a Card in main()

Lets now try and create a new instance of Card and pass it the CardNumber::Two of CardSuit::Spades. Compile and make sure there are no errors.


Test Getters

Now lets output our number and suit to test our getters.


Run Project

Lets run the project and make sure we see the 2 of spades printed out on