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


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

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

  • Tony

    At first my solution was similar but my Monster struct had only two members (name & health). Thus, my program was much more complex. After looking at the solution I realized that having a MonsterType type member in the struct was actually the best.

    By the way, to find out the actual "solution" myself I had to check chapter "Enumerated types" again (the example where it uses std::string as a return value of the function). Is that normal at all? Couldn't get the std::string part on my own apparently!

  • AE35_Unit

    I had to look at the solution several times. Unfortunately I struggled with this. I'm sure it's the fact we have a an enum inbedded in a struct. I would have liked to have seen more guidance on such things in the lessons.  Thanks for chapter though.  I'm pressing on to chapter L.5.x as it looks like quite a lot that I'm comfy with. See ya there, cheers.

    Written with Geany on a raspberry pi 4

  • sami

    Is the following OK, if I want to initialize all the struct members?

    • nascardriver

      Unless you use the initial values, use empty curly braces. For `name`, use empty curly braces in either case. Passing it an empty string literal is slower.

  • Yung

    Would you please elaborate on this statement after 'where all of ...' ?

    >>Enumerated types let us define our own type where all of the possible values are enumerated.

  • CvB

    Hi Alex and nascardriver,

    I've noticed that a lot of comments/questions pertaining to this quiz are listed under the comments for summary and quiz of chapter 6. I assume they got misplaced while reordering the lessons. Maybe you can look into it and transfer the appropriate comments to this lesson.

    Cheers and keep up the good work!!

  • Cerezas

    Changing exercise function prototype:

    to:

    would improve reusability.

  • Hiep

    In the quiz solution, enum class MonsterType was declared before std::string name in Monster struct declaration. However when I try to print out the size of enum class and std::string

    This is what I get

    Will this affect data structure alignment?

    Declaring enum class first before std::string

    This will print

    Declaring std::string first before enum class

    This will print

    • nascardriver

      Good call. Alignment isn't something we care about in the tutorials, and also in practice it is mostly ignored unless memory usage is of concern. Having a logical structure of members by what they're used for is often more helpful than saving a couple of bytes.

  • Ef

    EDIT: Nvm I worked out that the issue only occurs with the ENUM Type

    For some reason my code doesn't let me print the attributes unless I use static casts:

    main.cpp
    #include <iostream>
    #include <string>
    #include "monsters.h"

    int main()
    {
        monsters::MonsterStats new_monster{DetermineType()};

        std::cout << "Oh no it is a " << new_monster.Name << '\n';
        std::cout << "It is a  " << static_cast<int>(new_monster.Type) << " type.\n";
        std::cout << "It has  " << new_monster.Health << " health.\n";
        std::cout << "And it does  " << new_monster.Damage << " damage.\n";

        return 0;
    }

    DetermineType.cpp
    #include <iostream>
    #include <cstdlib>
    #include "monsters.h"
    #include <time.h>

    monsters::MonsterStats DetermineType()
    {
        srand(time(NULL));
        int monster{rand() % 5};

        switch(monster)
        {
            case 0:
                return Ogre;

            case 1:
                return Dragon;

            case 2:
                return Orc;

            case 3:
                return Giant_Spider;

            case 4:
                return Green_Slime;
        }

    }

    monsters.h
    #ifndef MONSTERS_H_INCLUDED
    #define MONSTERS_H_INCLUDED

    enum class MonsterType
    {
        Ogroid,
        Reptillian,
        Arthropod,
        SLIME
    };

    namespace monsters
    {
        struct MonsterStats
        {
            std::string Name;
            MonsterType Type;
            double Health;
            int Damage;
        };
    }

    monsters::MonsterStats DetermineType();

    inline monsters::MonsterStats Ogre{"Ogre", MonsterType::Ogroid, 120, 10};
    inline monsters::MonsterStats Dragon{"Dragon", MonsterType::Reptillian, 120, 10};
    inline monsters::MonsterStats Orc{"Orc", MonsterType::Ogroid, 120, 10};
    inline monsters::MonsterStats Giant_Spider{"Giant Spider", MonsterType::Arthropod, 120, 10};
    inline monsters::MonsterStats Green_Slime{"Green Slimes", MonsterType::SLIME, 120, 10};

    #endif // MONSTERS_H_INCLUDED

  • Gabe

    Quiz

    • nascardriver

      `getMonsterTypeString` calls for a `switch`-statements. The compiler can optimize `switch`-statements better than `if`-statements. When you have a limited set a values (eg. enum or char) that you want to check for equality, use a `switch`-statement.

  • Dudz

  • Al

    You said that both `enum` and `struct` declarations could be included in a header file without loss of performance because their declaration doesn't allocate memory. Is this also true for:

    1. enums where we give explicit values to each enumerator,
    2. enum classes with or without explicit values given, and
    3. structs with default values?

    Thank you in advance.

  • Raton

    "Enum classes [...] don’t pollute the standard namespace quite as much."

    Don't you mean the global namespace? (third paragraph)

    • nascardriver

      "standard" is wrong, "global" is better, but an enum can be defined in a namespace that is neither `std` nor global. I changed it to "encapsulating namespace". Thanks for pointing out the mistake!

  • giang

    Hi, I tried to write a program to solve the quiz above but my program works well when I just use 'enum' instead of 'enum class'. When I tried 'enum class'and '::' operator, the compile threw me errors that said:'MonsterType is not a class or namespace'. Can someone explain this situation to me, please? Thanksss

  • 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.

    • hi

      I may have broken your program.

      output:

      What is your monsters name?
      Destoyer
      What is your monsters health?
      9999999999999999999999999999999999999999999999999999999999999999999999
      Which of these types is your monster?
      0. Ogre
      1. Dragon
      2. Orc
      3. Giant spider
      4. Slime
      answer with the number in front of the type
      This I don't understand. is named Destoyer it has 2147483647 health.

  • 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]