- A structure is an aggregated variable in C++.
It's a way of collecting
a number of disparate variables
in a single container.
Here's a working copy of struct.cpp
from chapter two of the exercise files.
This is a simple structure
that contains a few different types of variables.
You'll see that there's an integer, a double
and a c string.
The structure itself is simply this definition.
It's very much like an aggregate type
and it's used as a type in the code.
So here I use this capital s,
which is the name of our structure from right there
and I declare a variable s1
and I initialize it with an initializer list.
Now keep in mind the initializer list
is a feature of C++ 14 from 2014.
In previous versions of C++
you had to initialize each element separately
using dot notation so I would do something like this,
dot i equals three s1.d equals 47.9,
semicolon and s1.s equals string one
and I would initialize it like that
and I would just have the semi-colon there
and this would have exactly the same effect.
Likewise when I access the variables
I use that same dot notation
and so when I build and run this you'll see
that it displays these three variables as a integer,
as a floating point and as a string
and of course these variables can be both read
and written so I can say s1.d equals 73
and then when I build and run you'll see
that the double the floating point number is now 73.
So the structure is an important
part of C++ programming
and we'll see many more examples
throughout this course.