Search

8.x — Chapter 8 summary and quiz

Quick review

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 encapsulating 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!

Question #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


9.1 -- Arrays (Part I)
Index
8.5 -- Random number generation

77 comments to 8.x — Chapter 8 summary and quiz

  • Josh

    Hi, just a heads up. You might want to move the part about std::string to chapter 4 as it's no longer in this chapter

  • Rayyan Khan

    Super Happy with my solution ( I haven't checked the official one yet, but it has got to be similar!)

  • Jay R

    It took me a while to figure out why my program wouldn't allow me to use enum class. I used a reminder from chapter 9 to do a static cast. I know, it's not the easiest to read and I should have created a separate function for getting the monster type, but here goes nothing. Lol..

    Thank you Nascar and Alex :)

    • nascardriver

      When you add a monster to your enum, what happens?

      • Jay R

        This ogre is named Torg and has 145 health.
        Unknown monster type.
        Program ended with exit code: 0

        It makes my program behave in an unexpected way. I expected it to output the information for Blurp, but since slime is not included in my switch case the default case is executed.

        • nascardriver

          The problem isn't that you're not handling a slime, you have a case for slimes, but you've used magic numbers. You should only get "Unknown monster type" if you try to print a `snake`.
          Don't cast `m.monster_type` to an `int`, switch `m.monster_type` directly and use enumerators for the case labels.

          • Jay R

            Okay, I think I understand.. I added scope qualifiers to the case labels since I'm using an enum class. I hope this it what you mean. Here is the fixed code:

  • Medhat Diab

Leave a Comment

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