OK, so now we are going to talk about something that is going to be a little bit confusing, most likely,
but nevertheless this very important topic, and it will actually be a change in the style in which
we write our code and we're going to be using this style throughout the rest of the course.
So and this style is going to be using multiple files for our code rather than just putting everything
into one file.
And it kind of has a lot to do with classes and object oriented programming that we've just started
talking about.
So like I said so far, we've been smashing all of our code into one file, right?
So in this one file, we have our class definition, which was this right at the top of the file, we
had our class car and then the definition, which included the defined prototypes of the member functions.
And then we had our defined member variables, of course, and we separated it into public and private.
Below this, we had our member function implementation, so we saw those prototypes up there.
And these were the implementations right here, the constructor, and then we have the rest of these
functions with the actual code that goes inside of them, the actual implementations.
So that was in the middle of our file and at the bottom of our file, we defined our main function and
implemented it right here.
So this is something that you can refer to as the client code.
Well, when I say client code, we're talking about like the user of the car, right?
The person that creates the car or bias the car and uses it, not the engineers who blueprint the car.
That kind of stuff is up here in these two sections.
So what we're going to do now is we're going to break each one of these parts into three files because
they have three distinct parts.
We're going to separate it out.
And this is going to be these files right here.
So we have our blueprint class definition is going to be in a file called Khadka H.
We have our implementations for the member.
Functions are going to be in a file called card copy.
And our main function will be in a client file called maned CBP.
So first off, here, I when talk about this car dot h file, so this is something we refer to as the
header file.
So this file will contain the class definition slash blueprint.
The name is the same as the class, so we see a Capital C car is the same as the class name here and
those kind of small.
We can't read but same name as the class, but then we have an extension h, which stands for header.
And this is the header file.
So all that stuff is going to go in its own special file, which is out of file.
Next, we have the implementation file, so this file is going to contain all of the class member function
implementation, so we saw the prototypes up here.
These are all of our prototypes in the header file now, and we're going to put the actual implementation
of them in a file called card copy.
So the name will be the same as the class name right car.
But instead of a dot h, we are doing a copy like we had before and this is what we stuffed everything
into.
Originally right was card of CBP.
So our extension will be keep the same name as the class.
So this third file is going to be what we consider the client file, the client file is just some code
that has an an interest in using the class.
But you know it is not the engineer portion, right?
It has no concern about how all of these functions operate.
It's just going to use them, right?
It's somebody built a car for a purpose, and this code right here is putting that to use.
Right?
So creating car is driving, the car is changing the paint, getting the odometer, all of that stuff.
So this is what we can consider client code and the client file.
Since we have main function in here, we're going to call this main step, but it doesn't doesn't always
have to be made up for client code.
You can have multiple files.
Just, of course, having our main function right in this file and we don't have any other files or
other client files at the moment, and it doesn't have to just be a main function here.
It could be a main and other functions as well.
So this is meant for a user or developer who wants to use the class, right, so it's someone who is
interested in using the car class just like we would with a string class or something like that.
You notice when if we include a string, we're going to use a string right and we might use string lengths
and all that stuff.
Now we are using a car.
So this client code is going to have to include the car class.
So we will see that.
And this next part right here where I jump in to the editor and I'll explain some other things.
So I head over here to a visual studio code, and we have our main CPI file card out CP and card up
each file in here.
So you've seen this already, but I'm going to explain this stuff up here.
So just like we include string to access some stuff with strings, right?
We got to create some string types.
We don't have any.
I'm not making any string types in here, but I just included as an example.
Just like we include string, we're going to have to include car, although it looks a little different,
right?
Something that I want to say right off the bat, though, is that when we are doing this, we are including
string.
We're actually including the header file.
Just like car dot h string has a header file and that is what we're doing.
We're actually including string.
And you can actually include something that string on h.
This is kind of more of a C programming language concept, but you know, we don't have to put the data
and we can just use this syntax.
But really, this is like a header file that we're including.
So this include Khadka age is pretty much the same thing as this, including the string header file,
but we're including our own header file since we're not including a se t.l a standard template library.
Kind of header file here, we have to put quotations because we created this, it's not part of the
standard template library of C++.
This is our own header file.
Therefore, we have to surround it with quotes.
It's just a little bit different syntax.
We're not going to use the less than greater than we're going to use these quotes to it whenever we
want to include our own header files for our class definitions.
And you might be thinking, OK, well, we include Carter H.
Do we not have to include Carter ACP?
Well, yeah.
For now, we're not going to have to include Carter CP.
You don't have to include any other stringed CP or something, right?
You're just including string.
And this includes string.
It just happens to be including the header file.
That's what it does.
So we're only having interests, including the header file.
The compilation and linking process will handle the rest of putting these three files together when
we get an executable.
So let me move on to the next one card out, C.P. So just like mean gossip.
We also have to include the header file in our implementation file, the card CPP file.
That is because we need access to the car blueprint, right?
The car definition to be able to implement these functions.
So we're going to use the quotes and include our Carter H header file.
Another thing I want to mention about String is that if you're using strings in your code, you might
as well just include string because you notice before that, I didn't always have to include string
to use strings in my code, but that is kind of a compiler specific thing.
I think men GW handles that in a specific way, and iOS Dream also has some stuff with strings, so
you might be able to get away with just including Io stream.
But if you want your code to not really be compiler dependent and be more flexible, you should include
string at all times whenever you're using strings at all in your code.
So I recommend putting this include string in there as well.
Of course, I don't necessarily need it in Maine because I'm not directly including them.
I'm not like using strings so I can get this out of here.
Plus, ice cream has some things that have to do with strings like we can just print out the strings
here and stuff.
So let's go ahead and head back here.
So we're pretty this is pretty much done here.
You know, this is just everything that you saw before.
It's all the function implementations.
This all just goes in its own file called the card CPP.
And the one main thing to point out right now, even though it wasn't discussed in the slideshow, was
that we need to include the car header file here as well, just like in made CBB.
So let's jump over to Carter Page.
So this has some other interesting stuff.
Notice that I'm also including string here because I'm using strings, so just kind of reiterating that
point.
But what is this stuff up here?
OK?
This is kind of interesting.
These things you might have seen them before, you might not have.
These are called macro guards or pre-processing directives.
So what this actually stands for up here is, if not defined, it says if in death.
Right.
So if indeed, if that stands for if not defined here, what we do is just kind of a syntax thing.
We put all caps car underscore.
You do not have to put it like this, but this is kind of the standard.
So this is the name of our class and the name of our file right there because they're both supposed
to be the same name.
So that's why we put the capital car.
We do an underscore and then an h to specify that this is a header file and it's really defining some
sort of symbol, some sort of constant for this header file.
And why do we need to do this?
Also notice down here says, and if this is another part of this processing directive thing.
So why do we need to do this?
Well, the reason is because if we start including multiple files like, let's say, we include another
file in here that we made, like maybe we want to make a tire class for the car and we have a private
data of its type tire our own abstract data type.
So if we want to make a private member here, we ought to include Dot H.
The problem is is if we include if we go to Maine, we include car dot h and we also include tire age
and here as well because we want to also just make tires or something.
There will be this problem in the compilation and linking process, and it will be this double include
problem is basically going to just try and process do pre processing of the header file twice.
Because it's basically going to it'll go in here, it'll be oh, tired out is is here and it will jump
over and check out tired.
OK, you included tired of age.
Let's just hypothetically say I have tired of age here.
I could just put that here just in case.
But so something like this.
So the typing is still quite a struggle.
So I include tired, right?
And it's like, oh, tired at age.
Let me check that out.
Jumps over to the tired of file checks that out.
And then what it does is goes in here and it has.
Let's say include tired age, and then it comes over here, and it's like.
So I guess I what I should really start out with is, let's say we're in Maine, right?
It is this include Carter, actually, because of here.
It's like, Ooh, Carter h.
Oh, tired, I was there.
I go, check that out, OK?
And I then I come back and I processed Carter H and I go over here.
You know, and I have tired of age.
It's already process, tired of age, you know, and it doesn't really matter necessarily the order.
It can still be problematic.
It's just going to try and process this tired age again so that that becomes a serious problem.
And it still is good to have these pre processing directives, even if we don't have Carter age or sorry,
we don't have time to age.
It really just protects us with all of this including stuff, because if we start including stuff in
different places, you know, we're including some stuff in the client code or including some things
in here or including some things in here.
It really just protects us.
So there's not this like double pre-processing of the same header file multiple times that will lead
to linker errors and quite often linker errors that you see will be very ambiguous and hard to understand.
So the linker is part of the compilation process, right?
The files get compiled and then after they're compiled, they get linked together.
So.
That's just something that we might talk about more later, but right now, I'm not incredibly important
to you.
But what we need to do is now compile all three of these, so I'm going to go ahead and show you how
to do that.
So just remember that these pre-processing directives are important to put inside the header files because
they to protect us against that kind of double inclusion, double processing thing.
Get one last thing.
I guess this in-depth.
Basically, what happens if I can specifically describe to you what happens is?
We see this, if not defined car age, and let's say that it's not defined yet, then it'll say define
car age.
So if it's not defined already, then define it.
If it's not processed already, then we'll go ahead and we will process it.
Otherwise, we would jump over all of this code in here and go straight down to this.
And if so, it would not do anything in between.
It would not do any processing in between here if it's already defined right.
That prevents us from doing that, like double processing with the includes and stuff like that.
OK, so let's go ahead and see how we would compile this now, because previously we've just kind of.
Compile the one file, right, and how do we do that?
Instead, D-plus Plus and you know, it was like hard copy Dashiell out, easy, something like that,
right?
All we have to do now is to compile not only car first card copy, but main CPB afterwards.
So you do not need to compile the header files.
You don't need to compile.
I stream.
Do you write?
It just includes a header for you.
It just gives you access to this other library.
The link is going to handle all this stuff for you, the compiler and linker.
So you don't have to deal with that.
You know, it basically evaluates all these symbols, which we, you know, we'll call them, and it
will correctly put together your executable.
We just need to make sure that we're putting the G Plus Plus compiler statement in the correct syntax
in order.
So we're going to go ahead and put this card CP and then we're going to go ahead and put this main CP.
We do not need the card on h.
So we're going to compile all of the CP files.
So if you were to have more client files or you had another class like, let's say, we had the TYR
class, so we had or h and tired CP, you would not compile Typekit h, but you would have to include
tired CP here.
So you could just put tired CBP then car to keep in mind asleep and then we can leave this dash if we
want to name an output file.
So let's go ahead and compile this.
So it looks like everything is all good, and I will go ahead and run it just to show you that this
has the same output as what we've seen in the lectures before.
So if we go over to Main, we see what is expected here at car.
One miles is 40000, right?
That's what we're getting off the odometer.
That's what was set right here.
Car to get odometer is 10000 miles.
Then we notice that we have the jet fuel was 12 because that was kind of hard coded here in the constructor.
Then we have.
Blue Bell, excuse me, blue for the paint, for the get paint and then.
We sat down at eight thirty seven and we get the odometer says eight, thirty seven and then we get
the paint after this changing to black and this is black, so everything works the exact same.
What it did was it went ahead and went through all of these files and compiled and linked them correctly
into that executable, just as if we had only been using a one file like hard copy.
So this is probably a lot to take in.
This is a totally new style of programming, but this is kind of the way to do it.
This is the real software engineering way to organize your files in C++, and so that's why we're going
to start using it.
It'll become easier as you practice it and use this more.
It'll become fairly straightforward that whenever you're making a class, you're just going to make
a header file that has this stuff in it with the definitions.
Then you'll make another special file that has the same name, but it'll be dust up and it will have
all of the member function implementations in here.
And then you can make a client file that will include this header.
And now you have access to the car class and you can start doing whatever you want.
So we'll have access to whatever class we made with the header and the implementation.
OK, so that's all I have for this lecture.
In the next lectures, we're going to start using this stuff and we'll come up with some interesting
practice problems.
So just go over this as many times as you need to to understand it.
We're going to be talking more about classes and using this style, and there's a lot more to go over
with object oriented programming.
So we're just going to keep adding things into this header file and the implementation.
We're going to talk about overloading operators and stuff like that in the near future and then using
classes together like we're going to have several classes, not just car.
We'll have classes that relate to each other and they have a special relationship and and we're going
to talk about those concepts as well.
All right.
So with that, I will see you in the next lecture.