Structs
This lesson uses the previous Dog
struct example:
struct Dog
{
std::string name;
int age;
std::string breed;
};
Once we define the struct we can create variables with the type Dog just like any other types like int
or std::string
. Exclusively for struct or class where all its fields are public, the aggregate initialization can be used. All the fields are put in curly braces in the same order in the struct definition. For example:
Dog spike{"Spike", 78, "American Bulldog"};
// Sets name = "Spike", age = 78, and breed = "American Bulldog".
Your task is to create the variable children_book
and initialize it to have these values:
title
: "The Very Hungry Caterpillar"
author
: "Eric Carle"
year_published
: 1969