Structs

8.1 - Creating structs


C++ allows us to create our own data type, which contains member variables (also called fields) of various types. For example, we can create a data type called Dog which has name, age, and breed variables like this:

struct Dog
{
    std::string name;
    int age;
    std::string breed;
};

Notice the semicolon after the struct definition ended, failure to put a semicolon there spells disaster for you.

Your task is to create a struct called Book containing these fields:

  • title, an std::string.
  • author, an std::string.
  • year_published, an int.

Note: create the Book struct outside the main function. You should not create the main function for this lesson, the test cases will define it.

#include <string> // Create the Book class after this line!