(bright upbeat music)
- [Erin] We've learned a lot
about system defined data types.
So let's take a look at a user defined data type.
The first is the struct.
If we look back at the array,
the array has the ability to store multiple pieces of data
in one memory block.
The downside is,
all that data has to be of the same data type.
Similarly, a struct can store
multiple pieces of data together in one memory block.
This time, the data can be of different data types.
So there's no special library needed to define a struct,
we just say struct, and then give it a name.
Since we are creating a new data type,
it should be pretty generic.
Then inside the curly brackets,
we define the data for that data type.
Structs always end with a closed curly bracket, semi-colon.
In order to use your data,
you just say your new data type,
just like any other variable,
and then give it a variable name.
So if I have students who have names and GPA's
that I need to keep track of,
I could create two arrays, one of strings and one of floats.
But that would be quite tedious to maintain the indexing
so that students line up with the correct GPA.
Instead, I can define a student data type
that has a string for their name and a float for the GPA.
Then anytime I reference a particular student,
I can find their name and their GPA.
So each time I create a version of my struct,
that new variable gets a string name
and a float GPA available to it.
So if I have multiple students,
I can create student one, two, three, four
all having two data points to each variable.
So if they all have a variable called name,
how does the computer know which name I want to change?
This is where we introduce the dot operator.
The dot operator separates object from data part.
So our object is our student dot.
And then we reference that particular student's name or GPA.
Let's take a look at this in code.
So here I've defined my students as a struct
and it contains a name and a GPA.
So now in order to use my struct,
I need to first declare an instance of it.
So I can say, students and teacher,
that doesn't really make much sense
to have a teacher of type student,
but it will demonstrate how I can change the name.
Now, to access the teacher's name, I say, teacher dot.
And then I get populated a pop-up box
with the two variables
that I've defined for my data type students.
So now I can type in teacher
and reference the teacher's name
and change it as appropriate.
You can think of these new objects
kind of like twins or triplets.
Each of them may have the same GPA,
each of them may have the same structure,
each of them may have the same features
but how you assign them and change the data inside
is going to be what's unique.
Let's look at another example.
Let's say, I wanna create the entire class.
I could create a student variable for each student
or I could create an array of strokes, an array of students.
So if I have 10 students in my first period class,
all 10 of these spots have a name and a GPA.
So it's like copying this 10 times.
So in order to access the first student's data, I would say,
and remember our arrays always start their indexing at zero.
So the first person in my array is actually at index zero.
So if I say first period at index zero,
now I can say the name is equal to Tom.
And now I can set his GPA to 4.0.
You cannot assign all the data or one piece of data
to an entire struct.
So I had to reference the individual data points
because the generic struct doesn't understand
how to assign data to it.
If you have multiple data points,
and I said, first period at index zero equals Tom.
We understand there's only one string in our struct.
So take the string and put it in the name variable
but the computer doesn't,
you still have to tell the computer
exactly what to do with the data
so that it knows you're not doing something erroneous.
Also, you cannot print an entire struct.
So I cannot just print first period at index zero
because there's two different print functions
one prints strings, and one to print a float.
So if I just print an entire struct,
the computer has no way to know
which data point I'm referencing.
And it will give me an error.
Also, the computer doesn't know in what order
you want things printed.
So it's always best to just print them individually
and everybody should be happy.
So now, if I run this program, it should print Tom
because I have assigned Tom's name
to the very first position in my students array
and the name portion of that position.
(bright upbeat music)