All right.
Welcome back to another lecture, we are now going to get into a new kind of piece of the course where
we talk about abstract data types.
So abstract data types are things that are going to enable us to bring abstract ideas into our programs.
So when I say abstract, I mean things that are not as well defined like things that we've done so far.
Functions, loops, variables of types that are integers and boolean things that you were just told
how they work and what they do.
And they're kind of part of the language and you were just learning to program with these building blocks.
Now we're going to kind of create our own blocks, our own shapes, our own data types.
This is going to enable us to solve much bigger problems and turn kind of real world issues and real
world world things that we want to build into code.
So up until this point, we've just been dealing with pre-existing data types.
So you've been told what these data types are and what they do, like an integer or float, bool, char,
etc. all those things.
Now comes the time when we're going to make our own data types.
And why would we want to do this?
Well, it's because of what I was just talking about.
We want to represent abstract ideas in code.
A good example is the string data type that we've been using that is actually something that someone
made.
It wasn't originally like baked into the basic fundamental structure of the C++ language.
It was kind of added on was created by someone that wanted to extend the functionality and make programming
in C++ more powerful, more simple.
You kind of keep adding things on to a language and it becomes more abstract.
It becomes closer to spoken language or something.
You know, we're able to kind of use small pieces of code that are very powerful and do a lot, whereas
you used to have to write maybe 10 or even 100 times as much code as far as typing characters just to
do the same thing.
So the data types we create can be a lot more abstract and imaginative, so we're going to go ahead
and look at some simple, maybe somewhat trivial, but just to get us to understand some simple examples.
So let's think back to the string data type.
It had a member function called length, right?
We had to use the dot length to get the length right.
So that gave us the length of the string, of course.
So what if I told you to instead design a car data type?
This may seem weird because you're like, Well, my car's a physical thing.
You know, it's not like a code thing.
A string was like a code thing.
It's a virtual like digital data thing like the car's not that I can see a car, I can drive a car.
You can touch the car well.
We can represent the idea of what a car is in code.
So that makes me then have another question for you.
So what kind of information would be associated with that?
And then more specifically, what type of member functions like this might you have for that car data
type?
So you could, of course, have a length function, I mean, a car has a length just like a string,
right?
But can you think of things that are a little more specific to cars?
So if I said that this car could hold a collection of variables, so this car data type can hold variables
of other types that you already know, like and charbel and string, what values would you have in these
variables?
What would they be?
What variable names would you put for them?
Let's say you had maybe like an integer, a float in a string or something like that.
What would you put as the names for those if you were going to associate it as information that has
to do with card?
So let's think about the car data type.
What about the price of the car?
A car has the price right.
You could make a price variable for this car data type if you made a price variable, what data type
would that be?
Would you make it an end or would you make it a float?
What about the color of the car?
Would you make that a string?
So these are questions that you're going to need to answer when you're creating your own abstract data
types.
So the first thing that I'm going to discuss as far as implementing abstract data types is something
called structs.
So there's multiple ways to associate a collection of these variables that we're talking about that
have to do with a type that you came up with.
The first one that we go through is structs, it's kind of the most simple one, this was actually part
of the sea language before C++ came around.
So sea has struck, C++ has a lot more than what C++, but see kind of as far as the terms of abstraction
and kind of stopped its structs.
So here we have a struct in an example program.
Nothing is happening in Maine, so it doesn't really do anything.
But this is how you would declare a struct.
And so a struct you can just think of as a structure of data.
So we're saying, OK, we're going to have a structure.
So it's kind of like, you know, a shape or a building or, you know, some type of structure.
That's what it is.
So we say struct for short.
Here's what we call it.
We call it car, and we're going to talk about naming conventions in a bit.
You notice I have a lowercase right now, but that's not necessarily what we're going to do for our
names.
But I'm just keeping it kind of simple.
You can do this, you can name it all lowercase car.
And then we have some brackets, you know, we're very familiar with the brackets we use, the brackets
for the functions, right?
Like this main function, we use brackets for the F.
We used brackets for the for in the while, right when we're defining a struct.
You know, this is up here in global space.
It's a global variable.
It's accessible to main.
It'll be accessible to the rest of the program if we continue to put other functions in between the
struct in main.
This is going to be accessible throughout the whole file.
So what are we doing here?
Let's take a closer look.
So this struct has three member variables.
You've heard me say member functions like dot length for the string that was a member function right
of string, just like vector dot size, right?
When you make a vector, let's say a vector and you call it my vector and you do my vector size, that
dot size was a member function, remember?
So this drug has three what we're calling member variables.
And it's because they are associated, they are members of the Struct..
So these are price color and miles per gallon, which have just shortened to empty.
So how do we access or use these member variables?
You notice that they've been declared in here?
So you're saying, OK, well, we have a car, this car has three different variables inside of it.
It's got price, colour and miles per gallon.
Kind of looks like this car is this big box and inside the box, we have, you know, three other small
boxes.
This can all store data, so let's say the main big boxes here.
Car stores some data, right?
What does it store?
Well, it stores three other primitive data types.
In fact, this is not primitive.
The strings, not primitive, but the intent to float are.
But it stores three data types, right?
It doesn't they don't have to be primitive, they can be anything, you know, they could even be abstract
data types in here.
The point is the car just has three variables of various types in here.
Each one of those variables has its own type two, right?
So here we have car type and we have a price.
Which is an entity we have color, which is a string type, and we have per gallon, which is a float
type.
So the weather we're going to access these member variables is just like the dot length and the dot
size that I was talking about for those functions.
You can do the same thing for the member variables, so you notice that I'm using Dot, the dot operator
here.
Before we do that, though, just like creating a variable of type int in here.
And just like we might create an end inside of our main, we need to create a variable of type car.
So notice that here we are declaring a local variable inside main of type car, just like we would do
with one of these other types in Maine.
We could, of course, say string color and then we could say color equals blue.
But what we're doing here is we're saying, OK, I'm going to declare a variable of type car in Maine,
and I'm giving that variable the name C here.
So rather than like, you know, end my end, it's car.
See, this is the type.
This is the variable name.
The way that we access the variables, and in this example, here I am assigning a value to it after
accessing it.
Accessing happens with the DOT, so we have see which is our variable name, right?
This was a variable name for a car.
It was C with an instance of a car.
And we do see dot price.
So this is the dot operator.
It's similar to those member functions.
We are accessing the variable car dot price.
So imagine here when I say car, right?
So that gives me this box, this big box.
Then I say car dot, and then I have access to all three of these in here, and I say after the DOT,
I put color that gives me access to this specific box inside of the car box.
That's what you can imagine it as kind of as kind of a metaphor or analogy or something like that.
So we use this dot here for all of the different variables and we're setting them.
We now store the value 20000, the integer 20000 into the price member member where we're calling these
member variables.
So the integer twenty thousand is now getting stored and assigned to the price member.
Next, we have these string blue.
Remember, this is a string four color.
The string blue gets assigned to the color member of the car variable that we made, and then the float
22.5 gets assigned to the MPD miles per gallon float member of our car that we made, which was seen
here.
We're just printing those out.
Once again, we need to access them, right?
It does not matter that we're not assigning stuff.
Even if we want to print it out, we're going to have to use a dot to get access to anything inside
the struct.
So here we print to the console the car's price, the car's color and the car's mpg.
So I tried to be very kind of detailed in my explanation about this initial struct idea.
So I'm hoping that we can go straight into a practice problem right away.
So what I want you to attempt and I say attempt, you know, don't feel bad if you can't figure it out,
but I would like you to try to make a program.
And sorry, this kind of knot doesn't have correct grammar here, but make a program that asks a user
for their name, age and blood type.
So this is going to be like a patient.
Right?
So this information should be stored in a struct called patient.
And then that struct is going to need the appropriate member variables, I just said that a patient
needs a name and age and a blood type.
Right?
So remember what I just talked about.
You can access these variables by using patient whatever the variable name is, right?
So patient is the type.
Right.
So we're calling it patient, but let's say that you name the variable something else.
That's what this really means here.
So it's not going to be the word patient.
Let's just say you call it patient in the variable name is like.
I don't know pee or something like that.
We put patient pee, you would do pee dot and then the member name, right, the member variable name
to have access to that, even for storing input into them.
So if you're reading input in from the console, from the user, which you know how to do.
Remember that when you're doing that, you can act like this whole thing right here, let's say p dot,
whatever variable name that is going to be the same thing as when you just read into another variable.
Right, a normal local variable that you would have created.
So just want to let you know that so your output should look like this, you should make it look exactly
like this.
Of course, this is the user's input.
I'm putting my name and age and a blood type.
You don't have to put these things to test.
You can use whatever test data you'd like as the user.
But this should be the same.
It should say this, it should say.
And then after you enter it, it should say patient colon and the name age, colon in the age and then
blood type colon and the blood type.
All right, so go ahead and pause the video, head to your favorite editor and try and figure this out
for yourself, and then you can continue watching the lecture once you are done.
OK, so what I'm going to do is go over this before I continue the rest of the lecture, so I'm going
to go here.
And what I'm going to do is I am going to go into this program, so I think I can move that simply because
I already made that.
So what I'm going to do is I'm actually going to make a.
File on a call, it struck out CP, so it's going to be stretched us CP, what I'm going to do is include
extreme as I'm going to need that for sure, right?
And then what I'm going to do is, say, using name base study because I'm feeling lazy.
OK, now it's time to make our struct.
So what are we going to do?
We're going to say struct.
We're going to want to make a patient struct.
But what I want to say right now is that C++ has some style guidelines for naming things and capitalization
and all that.
And so I'm actually going to put a capital p for patient.
This is something we'll discuss in greater detail later on, but you need to make sure to follow the
style guidelines because that's kind of important.
People created these standards of style.
So you should have it be capitalized.
So I'm going to say struct patient.
You don't have to, but you should say capital patient.
What I'm going to do now is think about that blood type, the name the age.
So I'm going to say that the name is going to be a string.
So let's do string name and for age, because it's not going to be a float, write your whole number
of years old.
So then our blood type should also be a string.
I would call it a cha, but we have a I think and B and then O and then a b, right?
I think those are the four blood types.
So since there's a B and that's two characters, I'm going to go ahead and call this the string as well.
So I will say string blood type, and I'll just do some camel case right there for that.
The capitalization of every kind of second word, you know, or subsequent word.
So I'm going to go ahead and leave it at that.
Don't forget the semicolon here.
That's an important thing.
So now I have my struct.
What I'm going to do is make my main function and then I am actually going to read into these variables.
So I will see out something first.
I will say, you want to say, enter your name, age and blood type.
Know for blood type.
We have like a b o a b something like that.
All right.
So and then now I'm going to leave it at that and then I will see in two.
I will say, Oh yeah, we got to make our looks.
We got to make our struct right.
So what I'm going to do is make a struct variable.
So we're going to see type patient, right, because our struct is type patient.
I'm going to call that P.
Now what we can do is read into these variables by using the dot, right.
So I'm going to do p dot name.
So we're going to read into that.
Then I'll do p that age or in into that and then I'll do P Dot blood type.
All right, now, all we've got to do is print it out, so I'm going to see out and I'm going to say
the name and then I'll put a little space here and I will say Peacock name to print that out.
I do doing in line.
I'll say, see out.
Yeah, age and repeat what age in line, and then I will see out blood type.
And this will be that blood type.
And I'll do an airline and then just to return zero and finish that up right there.
So that is all we should really need.
So we're asking the user for the name, age and blood type.
We specify this these options here, and then we go ahead and read all those in and then we print them
out in an order that was shown.
So let's go ahead and try and run this.
So I'm going to say G-force Plus struck that CBP dash.
Oh, and I'm actually going to call this uh, I think I, I remove my executable before.
It doesn't really matter, but I'm to call it patient out.
So I'm going to say G Plus Plus stretched out CBP and then Dash.
Oh, and patient got out.
All right, so it looks like that.
Comp. OK, so let's go ahead and run it.
So in a name, age, blood type, I'm going to say killing seven and a b, and there we see a printout.
You can go ahead.
And again, I'll just say in a, uh, five type O and then all prints out nicely.
OK, so pretty simple exercise was really just to get you to make your first struct and start feeling
a little bit comfortable with the syntax.
So let's head back to the lecture.
So some important final notes that I want to go over on strokes and abstract data types in general.
Here we have a stark rectangle.
It has on the same line being declared width and height.
These are two variables we have and width and height.
You can just put them on the same line.
You not have to do int width, semicolon and then into height semicolon.
On the next line, you can just put it all on one line.
Not sure if I've gone over that syntax before, but if you have stuff of the same data type, you could
just say and and then with height, something else, something else and else and so on, if they're
all integers.
So we have width and height and then I make a rectangle variable, right?
So type rectangle.
Notice the capitalization to for that, for our abstract data type name, and then I put a variable
name R1 and I make another rectangle R2, I say R1 dot with has the value of five now and R2 Dot with
has a value of five.
Then I'm like, huh?
They both have the with five, and I haven't said what the height is.
They're pretty much the same thing, right?
So can I say if R1 equals equals R2, then print out same?
No, you cannot do this.
You cannot compare structs to each other or any other abstract data types that we will be going over
in the future to each other by default.
Like this?
You know how you can compare an integer to an integer in a string to a string and a chart to a char?
That was fine, right?
Let's just say you had it with equals five and with two equals five.
Like two integers like that.
And you said with two equals with one, that's fine.
That's fine.
C++ is OK with that.
You cannot compare structs because they are more complex.
They have these inner variables and it's a user defined type.
C++ actually expects you to describe what this comparison really means in terms of your data type,
so you have to kind of create a new.
Equality operator and a new less than operator are greater than operators, so on and so forth.
And we're not going to get into that quite yet.
That's something that we're going to do with abstract data types pretty soon, not too far away.
But, you know, we're a little bit away.
Just want to let you know you can't do this in case you try this, you cannot compare two strikes directly
with these operators that you've learned these kind of, you know, less than greater than all that
stuff.
So don't try and do that.
Another thing is don't forget the semicolon.
So this has a semicolon.
After the brackets you notice, the other things you've done.
Look at the F here.
The if doesn't need a semicolon after the brackets, the loops don't need a semicolon after the brackets,
right?
Neither do functions, but the struct do need a semicolon after the brackets.
So just remember that.
All right.
So with that, this kind of I think enough to not overload you.
I don't want to go over too much in this lecture other than introduce you to the idea of an abstract
data type and one of the ways that you can implement your own abstract data type, which is a struct.
So with that, I will see you in the next lectures where we will continue on talking about abstract
data types.