Search

S.7.x — Chapter 7 summary and quiz

Quick review

std::string offers an easy and safe way to deal with text strings. String literals 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 don’t pollute the standard namespace quite as much.

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

Yay!

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. Use an enum class 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.

Show Solution


5.1 -- Control flow introduction
Index
S.4.7 -- Structs

20 comments to S.7.x — Chapter 7 summary and quiz

  • Lars

    Here is my attempt on a solution.

    monsters.h

    monsters.cpp

    main.cpp

  • Suyash

    Here is my solution to the quiz question... I have decided to split the code into 3 files: main.cpp, monsters.h and monsters.cpp files... The primary reason why I decided to modularize my code was to gain some extra practice by using header files to encapsulate different logical sections of a project.

    The code works as expected, without any kind of compilation/linking problems... I would love to hear your opinions about it and you are more than welcome to bring into my observation anything that missed my eyes...

    1. monsters.h

    2. monsters.cpp

    3. main.cpp

    • nascardriver

      Looks great, keep it up!

    • Lars

      A bit more elegant than my attempt. I like it :)
      One question:
      In monsters.cpp line 22 is it best practice to specify namespace when calling getTypeString? More specifically, is it best practice to use
      (a) monsters::getTypeString, or
      (b) getTypeString
      ?

      • nascardriver

        (b)
        You're already in the namespace, there's no need to specify the namespace. Just like you don't use `::` whenever you access the global namespace and you don't say "on earth, on this continent, in this country, ..." every time someone asks you where something is.

  • fxr

    This is my attempt a the quiz I opted to have user input for monster creation any suggestions on how to improve it?

    • nascardriver

      - Inconsistent formatting. Use your editor's auto-formatting feature.
      - Initialize variables with brace initialization for better type safety.
      - Use single quotation marks for characters ("\n" vs '\n')
      - Line 21+: Magic numbers in strings, use your enum.
      - Your function names aren't great. "listMonsters", "getMonsterName", "getMonsterHealth", "getMonsterType", "getMonsterTypeName" or similar are a lot more useful.

      • fxr

        thank you very much for the response will keep all of that in mind

      • fxr

        Hello, it's me again nascardriver I was looking at my code improving the things you mentioned I have a question how could I use my Enum instead of magic numbers for the selection of a monster type.
        Heres the part I'm talking about.

        • nascardriver

          I thought you'd ask that, but I answered the same question the day before and couldn't find my reply, so I hoped you'd figure it out. You can cast your enumerators to `int` to make them printable, then just replace your magic strings with the `int`s.

          Now, when you move around your enumerators, the list is correct.

  • kavin

    I made one like this since its asked in the question to print out struct members. Its kind of big but, is there any way to make this simple ?

    • nascardriver

      The `struct` should describe the data that it holds, but not the values (eg. names of the monsters).

      then pass only a `Monster` to `printMonster`, no other parameters.

      Try rewriting your code that way. You seem to have misunderstood the purpose of structs and enums.

      • kavin

        I am going nuts with this from morning and still can't figure out how to pass all values at once using just 1 argument like you said and print all the results in 1 function ! I understood the use of struct part you said but not the other thing .

        Sorry for being a super noob and firing series of questions at you. I don't want to move to next chapter without understanding this correctly !

        • nascardriver

          > Is this correct ?
          If you want to create multiple monsters, you need an array. You're declaring `monsters` as a single `Monster`, but trying to initialize it with multiple `Monster`s. That doesn't work.

          Inside of `printMonster`, you can access the monster the same way.

  • hausevult

    Woah, there are so few comments here! Well I like the way you use structs, really illuminating.

  • Kyle

    There is no link to the next lesson here.

Leave a Comment

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