8.x — Chapter 8 comprehensive quiz

In this chapter, we explored the meat of C++ -- object-oriented programming! This is the most important chapter in the tutorial series.

Quick Summary

Classes allow you to create your own data types that bundle both data and functions that work on that data. Data and functions inside the class are called members. Members of the class are selected by using the . operator (or -> if you’re accessing the member through a pointer).

Access specifiers allow you to specify who can access the members of a class. Public members can be accessed directly by anybody. Private members can only be accessed by other members of the class. We’ll cover protected members later, when we get to inheritance. By default, all members of a class are private and all members of a struct are public.

Encapsulation is the process of making all of your member data private, so it can not be accessed directly. This helps protect your class from misuse.

Constructors are a special type of member function that allow you to initialize objects of your class. A constructor that takes no parameters (or has all default parameters) is called a default constructor. The default constructor is used if no initialization values are provided by the user. You should always provide at least one constructor for your classes.

Member initializer lists allows you to initialize your member variables from within a constructor (rather than assigning the member variables values).

In C++11, non-static member initialization allows you to directly specify default values for member variables when they are declared.

Prior to C++11, constructors should not call other constructors (it will compile, but will not work as you expect). In C++11, constructors are allowed to call other constructors (called delegating constructors, or constructor chaining).

Destructors are another type of special member function that allow your class to clean up after itself. Any kind of deallocation or shutdown routines should be executed from here.

All member functions have a hidden *this pointer that points at the class object being modified. Most of the time you will not need to access this pointer directly. But you can if you need to.

It is good programming style to put your class definitions in a header file of the same name as the class, and define your class functions in a .cpp file of the same name as the class. This also helps avoid circular dependencies.

Member functions can (and should) be made const if they do not modify the state of the class. Const class objects can only call const member functions.

Static member variables are shared among all objects of the class. Although they can be accessed from a class object, they can also be accessed directly via the scope resolution operator.

Similarly, static member functions are member functions that have no *this pointer. They can only access static member variables.

Friend functions are functions that are treated like member functions of the class (and thus can access a class’s private data directly). Friend classes are classes where all members of the class are considered friend functions.

It’s possible to create anonymous class objects for the purpose of evaluation in an expression, or passing or returning a value.

You can also nest types within a class. This is often used with enums related to the class, but can be done with other types (including other classes) if desired.

Quiz time

Question #1

a) Write a class named Point2d. Point2d should contain two member variables of type double: m_x, and m_y, both defaulted to 0.0. Provide a constructor and a print function.

The following program should run:

This should print:

Point2d(0, 0)
Point2d(3, 4)

Show Solution

b) Now add a member function named distanceTo that takes another Point2d as a parameter, and calculates the distance between them. Given two points (x1, y1) and (x2, y2), the distance between them can be calculated as std::sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2)). The std::sqrt function lives in header cmath.

The following program should run:

This should print:

Point2d(0, 0)
Point2d(3, 4)
Distance between two points: 5

Show Solution

c) Change function distanceTo from a member function to a non-member friend function that takes two Points as parameters. Also rename it “distanceFrom”.

The following program should run:

This should print:

Point2d(0, 0)
Point2d(3, 4)
Distance between two points: 5

Show Solution

Question #2

Write a destructor for this class:

Show Solution

Question #3

Let’s create a random monster generator. This one should be fun.

a) First, let’s create an enumeration of monster types named MonsterType. Include the following monster types: Dragon, Goblin, Ogre, Orc, Skeleton, Troll, Vampire, and Zombie. Add an additional MAX_MONSTER_TYPES enum so we can count how many enumerators there are.

Show Solution

b) Now, let’s create our Monster class. Our Monster will have 4 attributes (member variables): a type (MonsterType), a name (std::string), a roar (std::string), and the number of hit points (int). Create a Monster class that has these 4 member variables.

Show Solution

c) enum MonsterType is specific to Monster, so move the enum inside the class as a public declaration. When the enum is inside the class the “Monster” in “MonsterType” is redundant, it can be removed.

Show Solution

d) Create a constructor that allows you to initialize all of the member variables.

The following program should compile:

Show Solution

e) Now we want to be able to print our monster so we can validate it’s correct. To do that, we’re going to need to write a function that converts a Monster::Type into a string. Write that function (called getTypeString()), as well as a print() member function.

The following program should compile:

and print:

Bones the skeleton has 4 hit points and says *rattle*

Show Solution

f) Now we can create a random monster generator. Let’s consider how our MonsterGenerator class will work. Ideally, we’ll ask it to give us a Monster, and it will create a random one for us. We don’t need more than one MonsterGenerator. This is a good candidate for a static class (one in which all functions are static). Create a static MonsterGenerator class. Create a static function named generateMonster(). This should return a Monster. For now, make it return anonymous Monster(Monster::SKELETON, “Bones”, “*rattle*”, 4);

The following program should compile:

and print:

Bones the skeleton has 4 hit points and says *rattle*

Show Solution

g) Now, MonsterGenerator needs to generate some random attributes. To do that, we’ll need to make use of this handy function:

However, because MonsterGenerator relies directly on this function, let’s put it inside the class, as a static function.

Show Solution

h) Now edit function generateMonster() to generate a random Monster::Type (between 0 and Monster::MAX_MONSTER_TYPES-1) and a random hit points (between 1 and 100). This should be fairly straightforward. Once you’ve done that, define two static fixed arrays of size 6 inside the function (named s_names and s_roars) and initialize them with 6 names and 6 sounds of your choice. Pick a random name from these arrays.

The following program should compile:

Show Solution

i) Why did we declare variables s_names and s_roars as static?

Show Solution

Question #4

Okay, time for that game face again. This one is going to be a challenge. Let’s rewrite the Blackjack game we wrote in chapter 6 using classes! Here’s the full code without classes:

Holy moly! Where do we even begin? Don’t worry, we can do this, but we’ll need a strategy here. This Blackjack program is really composed of four parts: the logic that deals with cards, the logic that deals with the deck of cards, the logic that deals with dealing cards from the deck, and the game logic. Our strategy will be to work on each of these pieces individually, testing each part with a small test program as we go. That way, instead of trying to convert the entire program in one go, we can do it in 4 testable parts.

Start by copying the original program into your IDE, and then commenting out everything except the #include lines.

a) Let’s start by making Card a class instead of a struct. The good news is that the Card class is pretty similar to the Monster class from the previous quiz question. First, create private members to hold the rank and suit (name them m_rank and m_suit accordingly). Second, create a public constructor for the Card class so we can initialize Cards. Third, make the class default constructible, either by adding a default constructor or by adding default arguments to the current constructor. Finally, move the printCard() and getCardValue() functions inside the class as public members (remember to make them const!).

A reminder

When using a std::array (or std::vector) where the elements are a class type, your element’s class must have a default constructor so the elements can be initialized to a reasonable default state. If you do not provide one, you’ll get a cryptic error about attempting to reference a deleted function.

The following test program should compile:

Show Solution

b) Okay, now let’s work on a Deck class. The deck needs to hold 52 cards, so use a private std::array member to create a fixed array of 52 cards named m_deck. Second, create a constructor that takes no parameters and initializes m_deck with one of each card (modify the code from the original createDeck() function). Third, move printDeck into the Deck class as a public member. Fourth, move shuffleDeck into the class as a public member.

The trickiest part of this step is initializing the deck using the modified code from the original createDeck() function. The following hint shows how to do that.

Show Hint

The following test program should compile:

Show Solution

c) Now we need a way to keep track of which card is next to be dealt (in the original program, this is what nextCardIndex was for). First, add a member named m_cardIndex to Deck and initialize it to 0. Create a public member function named dealCard(), which should return a const reference to the current card and advance m_cardIndex to the next index. shuffle() should also be updated to reset m_cardIndex (since if you shuffle the deck, you’ll start dealing from the top of the deck again).

The following test program should compile:

Show Solution

d) Next up is the Player. Because playerTurn and dealerTurn are very different from each other, we’ll keep them as non-member functions. Make Player a class and add a drawCard member function that deals the player one card from the deck, increasing the player’s score. We’ll also need a member function to access the Player‘s score. For convenience, add a member function named isBust() that returns true if the player’s score exceeds the maximum (maximumScore). The following code should compile:

Show Solution

e) Almost there! Now, just fix up the remaining program to use the classes you wrote above. Since most of the functions have been moved into the classes, you can jettison them.

Show Solution

9.1 -- Introduction to operator overloading
8.16 -- Timing your code

397 comments to 8.x — Chapter 8 comprehensive quiz

  • solution to 1a:

    I note that Alex has used a const in his print() function but I don't see the necessity of that in such a simplistic example.  Maybe I am wrong here?

    • Hi Nigel!

      Every member function that doesn't modify members should be marked const.
      If you don't want me to make suggestions please let me know so I don't waste my time.

      * Line 10, 11: Initialize to 0.0
      * Line 15, 29: Uniform initialization

      • Thanks nascardriver. , yes I get that now.

        Line 29 isn't uniform because I just copied the main() routine from the question but the others should rightly be. I'll try to slip out of my bad habit in future.

  • Zac

    One comment regarding your shuffling algorithm. I read the Fish-Yates shuffle page in Wikipedia,–Yates_shuffle#The_modern_algorithm.
    In their algorithm, it picks an element from the unshuffled list and removed it from the shuffled list (and push this element to the shuffled stack). For instance, it first randomly pick a card from 0-51, swap that card with the card 0. Then it randomly picks a card from 1-51, swap the card with card 1. And repeat this for 51 times. This method provides a unbiased permutation.
    However, your shuffle algorithm above is different in that you choose the next element from not only the unshuffled part but all part of the deck, possibly creating an biased permutation.

  • Kumar

    Minor difference in what I was doing to what you were doing. I had the following declaration/definition in "class Deck".

    This is what I had written based on previous lessons. I thought, the "constexpr" better suits a constant here as the deck size for all decks is 52 cards. I wasn't too sure about the naming conventions, so I went with the "m_" prefix for the constant, as it is a member of "class Deck".
    Is doing this valid and is it good practice? Please provide feedback.


    • Alex

      constexpr here is fine, as that variable can be a compile-time constant. Using the m_ naming prefix is fine if m_deckSize is a member variable in your class.

  • Arumikaze

    Hello! Please give me feedback on my code. Thank you!

    • Hi Arumikaze!

      * Line 12: Using an enum here is alright, because it's nested inside a class.
      * Line 31: Pass class types by const reference (@std::string)
      * Line 70-72, 83: Uniform initialization
      * @getRandomNumber could be private, because it's not needed outside of @MonsterGenerator

      Good names, good structure, good use of const, keep it up!

  • Louis Cloete

    I have a question. I use Code::Blocks 17.12 with the GNU GCC compiler. However, I also had to discard the first value returned by rand() to get my answer to Quiz 3 to behave properly. Why is that? I understood that is a problem only affecting Visual Studio. I am attaching the program code as well as a batch file running it 1 000 times. Install the .bat file in the same directory as the build directory and then run the .bat file. Compare the results if you run it with and without the commented out line in main().

    8_x_Q3_RandomMonsterGenerator.cpp, resulting in 8_x_Q3_RandomMonsterGenerator.exe

    Batch file:

    Thanks in advance.

    • Alex

      Sounds like the issue with the first call to rand() impacts more than just Visual Studio. I'll update the lesson on random numbers to note this.

      • I recall something about GCC not using GNU libs on Windows by default and the libs are supposedly what's causing the problem, so this problem might apply to everyone compiling on Windows.

  • Somebody62

    Referring to question 3g, wouldn't this be a simpler function to find a random number?

    However, this function might not be perfectly weighted, so you might favor the old one.

    Thank you!

  • Sachees

    I cannot compile the Blackjack code from question 4(the one mentioned before 4a). I use visual studio and the compiler tells me that... "cout" isn't member of "std"! I copied the code directly from website and I added >>#include "stdafx.h"<<.

    • Somebody62

      This message is usually caused by forgetting "#include <iostream>" (without the quotation marks) at the top of the file. Please check to see if you forgot it or maybe it didn't get copied in.

      • Sachees

        This wasn't the case. I didn't expect that "#include "stdafx.h"" has to be in the first line of program, before everything else. Now it works.

  • Jack

    Noticed that, for quiz 3, the enum has all the MonsterTypes capitalised as if they were proper nouns (Ogre, Dragon, etc.), yet a couple of lessons ago in 'Nested Types in Classes' you had them in all caps (APPLE, BANANA, etc,). Is this a case of personal preference, or is there a standard/good practice that should be followed? I would have assumed all-caps is better as it is less likely to be confused for a class, which you mentioned should start with a capital letter?

    • nascardriver

      Hi Jack!

      There are many coding conventions. Google's C++ guideline suggests naming enumerators like constants and constants either all caps or starting with a lower case 'k' followed by camel case.


    • Alex

      For consistency I've capitalized them. However, because they live in the namespace of the class, they're unlikely to be confused for a class because nested classes are disallowed.

  • Liam

    Question 3f: What is the advantage of making MonsterGenerator its own class? Why shouldn't I make it a member function of class Monster? Or even just a standalone function?

    • Alex

      The advantage is mostly organizational. The process for generating a monster is different than the act of being a monster, therefore it makes some sense that these might be represented separately. That said, there's no strict rule here -- if the MonsterGenerator is trivial, you certainly could make it part of Monster itself. In this case, it's not trivial.

      I tend to use loose functions outside of a class when there is a collection of loosely related functions (e.g. a math or physics library). I tend to use static classes when I'm implementing something that has a specific job (in this case, generating monsters). There's some interesting discussion around this point here.

  • Serial Seth

    Why are the solutions to quiz question #1 passing the parameters by reference and while doing that adding the const type? Aren't we only wanting to to print 5? Wouldn't pass by value do the trick since both objects are created in main? What am I missing?

  • Nysnard

    How is it possible that sqrt() works even if I didn't include cmath? I am using Visual Studio 2017.

  • Cumhur


    if m_deck was not an array(assuming in std::array pointer is also carried), was an int value for example; we should have returned '*this' right?
    If we did, could we still return in constant?

    • Alex

      If I understand what you're asking correctly, no, you can't return *this, because *this refers to a Deck object, not a Card.

  • Dennis

    There seems to be a mistake with the solution for 4a. At line 93 we include iostream.
    This line should be at the very top of our program. Because class Card uses std::cout.
    Otherwise this program won't compile.

  • radu f

    Hi Alex,

    I wonder if adding a question like:
    - "considering the blackjack game program, how many classes do you think you should use to rewrite the code and why?"
    would be a good idea, before 4a-4d.

    Of course, there's nothing preventing us, the readers, to do that anyway...

    Thanks for your awesome tutorials.

    • Alex

      I didn't ask that question because it's hard to answer prescriptively. You should definitely have a Card class, and a Deck class. However, the Blackjack game itself could be a class, or not. There may be other classes or subclasses that could be created (I can't think of any, but maybe that's just my lack of imagination).

      I don't want users to get the impression that there's only one answer.

  • Matt

    This has been a lot of fun! I'm surprised, though, at how many more lines of code this took than the original implementation.  For this quiz I started with my own code from ch.6 instead of Alex's code on this page.

    By the way, @nascardriver, I found a way to add guidelines to Visual Studio.  It requires adding an extension, but I found it extremely helpful for this project:

    Alex, thanks for your awesome tutorials, and nascardriver, thanks all of your help along the way.  I never would have guessed I'd be able to write this code a couple months ago!