- [Tutor] The struct type provides a way to
aggregate a fixed set of data members into a single object.
Here I have a working copy of struct.cpp
from chapter three of the exercise files,
and here we have a structure
using the keyword struct to define it.
There's three data members,
an integer, and two C strings,
really any valid type may be a member in a struct,
and down here, we declare an object based on the struct.
And you see I'm using the name of the struct as a type.
I can use the keyword struct,
or I may omit it either way, it builds just fine.
I'm using an initializer list here,
to initialise the structure.
In versions of C++ before 2011,
the initializer list was not available for this,
so you often see structures simply declared,
and then initialised with each individual member,
being initialized separately like this,
instead of the initializer list,
and again, that builds,
and runs just exactly the same as with the initializer list.
Members are addressed with the dot notation,
like I used when I initialised.
So we have, Joe.name, Joe.role, joe.id.
If you're accessing a structure via a pointer,
you use a different syntax for getting at the elements.
So if we declare a pointer here,
now I can get out these with J,
and the pointer dereference Like this,
and it works exactly the same
we're getting at it through the pointer now,
instead of directly with the dot notation.
So the ability to create data structures using struct
is a fundamental tool in C++,
and as we'll see later,
it's also the basis of classes, and objects.