Search

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
Index
8.16 -- Timing your code

397 comments to 8.x — Chapter 8 comprehensive quiz

  • Brad

    Just a point of curiosity in regard to Quiz 2 and the destructor for the older than C++11 version. I thought that you'd have to create a for-loop to iterate through the char array and wipe the array that way. Is this unnecessary because we are using a destructor and the object will be deleted when the destructor is done doing its thing? Kind of like how it's not necessary to return m_data to nullptr?

    • Alex

      When you release (delete) memory back to the operating system, you're under no obligation to clear its contents first.

      If you look at the constructor, the only thing different about the C++14, C++11, and older than C++11 version is how the initialization of the value is done. The allocation of memory is the same. Therefore, it makes sense that in the destructor, the deallocation of memory can also be done the same way in each case.

  • Dom

    Hi Alex.

    On Q4 I'm trying to print the card last dealt using this code:

    However I'm getting a compiler error on the first line;

    "Error    C3867    'Deck::dealCard': non-standard syntax; use '&' to create a pointer to member"

    I don't really understand why this is the case. I also previously attempted to print the card by chaining the printCard function by making it return a *this pointer but the compiler also threw up errors with every single const member function. Any ideas?

    (It may also be worth noting that I have separated the classes into their own headers and .cpp files, but the program works fine without trying to introduce a method to show the card dealt).

  • Hardik

    Don't You Think We should use an anonymous object here?
    Better, No?

    • Alex

      I'm not sure what you mean. All you've done is remove the temporary variable. There are no anonymous objects here, and I'm not sure why we'd need one.

      • Hardik

        My ultimate suggestion was that it is good to prevent the creation of a temporary variable !
        As you also say, we shouldn't create unnecessary variables :) !

        • Alex

          > My ultimate suggestion was that it is good to prevent the creation of a temporary variable !

          It's best to do whatever is clearest for code comprehension. Temporary variables can help document what your code is doing, making it easier to understand. They also make your code easier to debug since you can see intermediate values. The compiler will probably optimize temporary variables away anyway.

          I probably wouldn't create a variable called "sum" just to add two integers together (doesn't really add value) -- but I would create a temporary variable to hold the result of a function call before I do something else with it.

  • Interesting, I did not know you could have multiple public or private sections in a class.

    And in problem 3d, what is the point of the empty set of curly brackets?

    • Alex

      The Monster constructor does everything it needs to do through the member initializer list, so the body of the constructor is empty. This is common for constructors that just do pure initialization.

  • Omri

    Hello Alex,
    Regarding:
    "It is good programming style to put your class *declarations* in a header file of the same name as the class, and define your class functions in..."
    Consider:
    "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..."

  • Felipe

    Hello Alex, on 3f-3h quiz wouldn’t it be better to include srand() and rand() inside the function since they need to be used every time anyways? is there a reason why this should not be done or is it just preference?

    • Alex

      srand() should only be called once per program, otherwise the results will not be suitably random. If you put it inside the function, it will get called every time.
      Discarding a random result with each call could, in theory, result in a skewed random distribution... but more likely, it will just result in wasted performance.

      So, no, you shouldn't do this.

  • Felipe

    In Quiz 2:

    In a normal function the *m_data pointer should be set to a null pointer once deleted?,

    , but inside a class do you need to do so also or you don't since it's a private pointer? Thanks for the great work.

    • Alex

      Generally speaking, you should set your pointers to nullptr after deleting them. However, the one exception is for deleted pointers inside a destructor. Because the entire class object will be destroyed immediately after the destructor finishes, in most cases you don't need to set your pointers to nullptr since they'll be destroyed subsequently anyway.

  • Ethan Hulinsky

    Try this

  • Satwant

    A class BlackJack for playBlackJack also.

  • Satwant

    can we create s_names and s_roars as class static member variables?

    • Alex

      You can, but it's generally better to define variables in the smallest scope possible. Since generateMonster() is the only user of those fields, it's better to put it inside generateMonster() rather than at the class level.

  • john

    Alex, why are getRandomNumber and swapCard in the deck class static functions?

  • AS

    I understand that to use a structure/class before it is completely defined(declared), we cannot refer to it directly. But we can use pointer to it.
    In solution 1(b), the declaration of distance() is taking the parameter of type "class Point2d". i.e. it is taking parameter of class type whose definition is still incomplete.

    Code compiles and works but How does it work? Shouldn't it give a compilation error ?

    • Alex

      Member variables must be complete. However, function parameters are allowed to be incomplete, so long as they have been declared. Without this, writing a copy constructor or assignment operator would be impossible.

  • Aakash

    Hello Alex,
    How can I define a variable in the class which is only modificable by constructor but not outside the class. The variable must be read-only outside the class.

    • Alex

      Make the variable a const member variable and ensure it is private. Making it const will mean it's only initializable by the constructor, but not after that. And making it private will mean only members of the class can access it. If you want outsiders to be able to access it, use an access function.

      • Aakash

        What I want is that it must be read-only outside the class but cannot be changed. Inside the class, any function can change its value. For eg, suppose I create my own string class. It contains an integer value Length. That value(Length) cannot be made const because it changes Everytime I use += operator, = operator, it's length changes every time but it cannot be modified outside the class. But outside the class, I must have access to it(like const) so that the following line compiles fine:

        • Alex

          It is not possible to have a member that meets all of the following criteria:
          * Is directly accessible outside the class (is public)
          * Is modifiable within the class
          * Is not modifiable outside the class

  • Michiel

    Hi Alex! I got a question on exercise 3h. My code keeps generating Orcs, but name, hitpoints and roar are always random
    Here's my code: