Tip:
Highlight text to annotate it
X
In this lesson we're going to introduce you to a really new, fascinating part of C++.
Up to this point, all you could declare in a program was the basic, built-in, primitive C++ types.
Shorts and ints and bools and strings and characters.
And there's not a whole lot that you can do what those individually.
Suppose that you wanna write a program to take care of the inventory of a business.
You can't hardly do that with ints and strings and characters, unless you were able to combine them in to
structures. Bags of variables that you could carry around.
And that's exactly what a struct is. So, a struct is your introduction to object oriented
programming. Get into a lot more details when we look at classes,
but this is just an introduction. It allows you to declare an abstract kind of object and so, we call them objects instead of variables.
So, you can build programs to better
simulate the real world. You can make types for tables and chairs and buildings and students and trees and whatever you want.
So, you are going to take different types and bundle them together into one
type that you name yourselves. Let's take a look at the syntax. For a struct, it is going to be the keyword "struct",
the type name, and then member variables.
So, in this case in our example, you're going to place this in its own header file called say, "point.h".
The keyword struct indicates to the compiler that this is a type that you are inventing. You're going to name it
"point" (that's the type name) and then your member variables
will be, the first one is a member variable of type float
and its name is "m_Xcoord".
Now, I prefer to use the "m_" as a prefix to the names of variables that are members of
structs and classes. Classes will come later.
That way when you see that variable name in the future in the program, you know that it's a member variable of some user defined type.
So, in this case we have two floats in "m_X", "m_Y",
and the last point I want to make is, don't forget that semicolon! The semicolon at the end of the definition is extremely important if you forget it,
you'll generate I don't know how many errors. Dozens upon dozens. I counted once, it's 73, but that was several years ago.
I don't know what it'll give you now. Don't forget that semicolon after the definition of a struct or a class.
So, let's take a look. You see what our definition for "point" is, it's two floats one for the X value, one for the Y value. Now, suppose that I declare
a point. Well the declaration is just like any other declaration. Just like you're declaring and int or a character or a bool or anything. It's always
type and name. Type and name. So, "p1" and "p2" are of type point. Each one of those objects contains two floats.
So, one float is what? It's 4 bytes. There are two floats, there are 8 byes in each, there are 16 byes of memory declared in this line of code.
So each one of these guys "p1" and "p2", have an x-coordinate and a y-coordinate. How to I access the member variables of an object
of type struct? You use a new kind of operator, it's called the "dot operator".
And there it is. To make sure you see it, I got an extra big arrow. To make sure that you understand that it is a dot, there it is and,
just emphasize, that is the operator. The dot operator, so
to assign to the x-coordinate of "p1", I
have to write then, "p1.m_Xcoord" and then the usual assignment.
I want to make something very clear to you. What is this? It is a point. A point object. But what is this? It is a float.
And you can treat it just like you treat any float any other instance.
So if I want to read into the y-coordinate the value six, there it is.
If I want to write it out, then I can write it out, as such. That's just a float.
Structs within structs. Well, of course you can use the type, what you've defined a type, say point here,
you can use that in another type. So, I can declare "line" as a type. So it follows the syntax: "struct line",
open and closed curly braces, don't forget the semicolon, and then it has member variables of type point.
So, again it's type and name. I name this one the left point of the line, and this guy the right point of the line.
That begs the question: how do we access the member variables of the line? Again remember that this is a line,
this is a point, and this is a float. So I can assign, as usual, with an assignment variable, eight to that value.
"my_line" is of type "line", each line has two points, a left point and a right point.
Each point has two coordinates, an x-coordinate and a y-coordinate,
and so if I'm going to assign to a line's left end point x-coordinate, the value five, that means five is going into that float position.
And likewise with the value eight. Here's another example. Again, and you have to imagine the much larger programs, suppose that
you're going to work for an auto supply company and you gonna run a database for them.
Each car part has many piece of information associated with it: a description of what it is, a part number, a wholesale price, a retail price,
a color, a manufacturer, a date received, quantity in stock, etc...
Rather than declaring all of these variables separate for 10,000 different parts, you can declare then a type in C++ called
"car_part". You do that with a struct definition. And then you can
use the various parts of that struct to define or describe an individual object.
And remember that definitions, this is what I call the definition of a new type, you want to put that in its own header file.
I say it's own header file because that way you can carry it from one project to another.
Now, for the purposes of learning and say, this class, with a struct, it's reasonable to simply put
that definition in a header for the entire programming project. Unless, of course you have many different
struct types. Then it might be wise to keep them separate.
What is a forward declaration? Well, let's take a look at this situation. If you were to write the definition of a line,
and it contains a point, then when the compiler gets to that
point in the compilation, it's not gonna know what a point is. You have the definition of point below it.
It hasn't seen that definition before.
Now, of course one solution to this problem is
swap them. Put the definition of point before the definition of line.
But, if for some reason that's difficult or you didn't think of it, or whatever the reason,
what you can create here is what's called a forward declaration.
This right here. "struct point;"
What you are telling the compiler is that this new type exists
it's going to be a defined eventually
don't worry about it. Just recognize it. When you see the identifier point, it is going to be defined.
That way, the compiler, when it sees "point' here in the definition of
"line" it's happy. It knows that eventually the definition of "point" will come forward.
Okay, here are some tips. Several years ago, I gave an assignment to some students and told them they needed to create a complex number
struct and then in a program they were to read in the name of the user of the program and use their name in subsequent prompts and output.
And there were several students who made the mistake of putting the name inside the definition of the struct.
That doesn't make any sense because complex numbers don't have names. And another mistake,
may forgot that semicolon. So don't forget that semicolon. And only put in a definition of a struct what is
pertinent. There are no names for complex numbers. They have only a real part and an imaginary part and that's it.
Even the "i" when you write Z is equal to A plus Bi. The "i" is simply a formatting element.
The A is the real part,
B is the imaginary part, and that's all that should be in the definition of that complex number. That that is the end of
this lesson, and I wanted introduce to you the fellow that has been helping me with the
sound effects in this lesson, you all know him, I'm sure.