4.x — Chapter 4 comprehensive quiz

Quick review

We covered a lot of material in this chapter. Good job, you’re doing great!

A block of statements (aka. a compound statement) is treated by the compiler as if it were a single statement. These are placed between curly brackets ({ and }) and used pretty much everywhere.

Local variables are variables defined within a function. They are created at the point of variable definition, and destroyed when the block they are declared in is exited. They can only be accessed inside the block in which they are declared.

Global variables are variables defined outside of a function. They are created when the program starts, and are destroyed when it ends. They can be used anywhere in the program. Non-const global variables should generally be avoided because they are evil.

The static keyword can be used to give a global variable internal linkage, so it can only be used in the file in which it is declared. It can also be used to give a local variable static duration, which means the local variable retains its value, even after it goes out of scope.

Namespaces are an area in which all names are guaranteed to be unique. Use of namespace is a great way to avoid naming collisions. Avoid use of “using statements” outside of functions.

Implicit type conversion happens when one type is converted into another type without using a cast. Explicit type conversion happens when one type is converted to another using a cast. In some cases, this is totally safe, and in others, data may be lost. Avoid C-style casts and use static_cast instead.

std::string offers an easy way to deal with text strings. Strings are always placed between double quotes.

Enumerated types let us define our own type where all of the possible values are enumerated. These are great for categorizing things. Enum classes work like enums but offer more type safety, and should be used instead of standard enums if your compiler is C++11 capable.

Typedefs allow us to create an alias for a type’s name. Fixed width integers are implemented using typedefs. Typedefs are useful for giving simple names to complicated types.

And finally, structs offer us a way to group related variables into a single structure and access them using the member selection operator (.). Object-oriented programming builds heavily on top of these, so if you learn one thing from this chapter, make sure it’s this one.

Quiz time!


1) In designing a game, we decide we want to have monsters, because everyone likes fighting monsters. Declare a struct that represents your monster. The monster should have a type that can be one of the following: an ogre, a dragon, an orc, a giant spider, or a slime. If you’re using C++11, use an enum class for this. If you’re using an older compiler, use an enumeration for this.

Each individual monster should also have a name (use a std::string), as well as an amount of health that represents how much damage they can take before they die. Write a function named printMonster() that prints out all of the struct’s members. Instantiate an ogre and a slime, initialize them using an initializer list, and pass them to printMonster().

Your program should produce the following output:

This Ogre is named Torg and has 145 health.
This Slime is named Blurp and has 23 health.

C++11 solution: Show Solution

non-C++11 solution: Show Solution

5.1 -- Control flow introduction
4.8 -- The auto keyword

178 comments to 4.x — Chapter 4 comprehensive quiz

  • mjr

    I've been looking over this and I don't know why it isn't working.

  • Outrageous Gem

    How would I sort this programm if I wanted to separate it into multiple files?

    "enum class MonsterType" and "struct Monster" could probably just be defined inside a header "monster.h" but where would I put "getMonsterTypeString(Monster monster)" ? Put it into a monster.cpp and forward declare it in monster.h?

  • emanon

    I have 2 questions.

    why did we have to use "monster.type" here? I dont really understand the use of the "."
    and the next question is,

    why did we use the "::" here?

    I probably should revisit where that was explained but I coudnt find it, it would be very kind if you can tell me which lessen to reread, thank you very much!

  • kaden

    my program is not working
    i can not find the reason for 2days.
    is there any problem in my code?

  • DAT


    enum class Monster

    struct MonsterDescript
        Monster type;
        int health;
        std::string name;

    std::string monsterType(Monster x)
        if (x == Monster::ORC)
            return "Orc";
        else if (x == Monster::ORGE)
            return "Orge";
        else if (x == Monster::SLIME)
            return "Slime";
        else if (x == Monster::DRAGON)
            return "Dragon";
            return "Unknown";

    void printInformation(MonsterDescript x)
        std::cout << "this " << monsterType(x.type) << " is named " << << " and has " << << " health.";
        std::cout << "\n";

    int main()
        MonsterDescript adam{ Monster::ORC,145,"Stupid" };
        MonsterDescript eva{ Monster::DRAGON,235,"Adam" };
        return 0;

Leave a Comment

Put all code inside code tags: [code]your code here[/code]