Learn C++ With Me #2 - C++ Program Structure

In Software Development

Learn C++ With Me #2 - C++ Program Structure - read the full article about C++ 2021, Software Development and from Tech With Tim on Qualified.One
Tech With Tim
Youtube Blogger

Hello everybody. And welcome to the second video in the C plus plus tutorial for beginners in this video, Im going to be discussing the C plus plus program structure, kind of the flow of code in C plus plus. So how certain lines of code are executed and in what order, and then Im going to be talking about all of the code we wrote in the first video in depth. So with that said, lets dive in to the video.

So in the first video in this series, we wrote this code right here as our first C plus plus program. Now, all this code did was just printing out to the screen that said, hello world. Now, what I want to do is go through this code in depth and discuss what every single line is doing and why we need it. So lets start with line number one. Well, line number one says include IO string. Now what include does is it takes a library of code and it tells our compiler that we need this for our program to run. It says all of the code thats written inside of this library.

You must include it in this program right here. So when we compile this program, all of the code thats written here will be included in this compilation. So be included in our executable file and the reason we need that so that we can access all of the code thats inside of here.

So in a library, there is a ton of code. And what a library is, is kind of a set of features or functionality that us as programmers can have access to. So for example, this IO stream library, this stands for input output stream. This allows us to input or get input from the console or output something to the console and a few other things as well. This is what allows us to actually use this right here, called see out.

So see out what its doing is character output writes out putting characters to the screen. However, this is not super simple to output something to a console.

In fact, this is probably, you know, hundreds of lines of code to actually allow this function right here to operate. Now we can take for granted that thats super complicated and we dont have to know how that actually works, but we have to understand that theres code underneath this that is actually allowing this to function. So we need to include all of that code in this file right here. Otherwise were not going to be able to use this C so hopefully thats kind of making sense, but the idea is theres all these built in features in C plus, plus all of these things that the C plus plus developers have done for us, for example, allow us to output stuff to the console and for us to be able to use that we need to take all of the code that they wrote and include it in our program. And we do that by using this include statement. So whenever you want to do or use some specific functionality, if its not by default built into C plus, plus you need to include it in your program. And usually you do this at the very top. That is why we have this included line. And its really all you need to understand about libraries. They are a collection of things that we, as programmers can use that the C plus plus developers have written for us. And you can also import other libraries that other people have the do stuff for you, right? That was the idea. All right, so now lets move on to line number two, it says using namespace STD. Now this is where it gets a little bit complicated.

So Im going to skip through a fair amount of this explanation just to really not confuse any beginners or intimidate everyone.

But inside of this IO stream library, there is something called a namespace, or theres actually multiple namespaces. Now a namespace is just a collection of code.

Its just a way to divide code up into, you know, kind of what its doing into specific areas. Uh, so that code thats related to one thing is going to be in one namespace code. Thats related to another thing is in another namespace. So in this example, theres this namespace called STD, which stands for standard. So inside of IO stream, there is a namespace defined called STD. And inside of that, namespace are all these functions like SCIO so console outer Sur characters out, and then theres another one called, which stands for characters.

And so that is whats defined inside of the namespace STD. Now, if we dont use this namespace STD, we still have access to the namespace because weve included it in our program. Weve included all of the code written in the IO stream library and inside the IO stream library.

Theres the namespace STD. However, if we want to use this, see out thing right here.

Well, we dont know where that is. Its defined inside of a namespace. So for us to build to UCO, we either have to explicitly reference what namespace see out is the find inside of in this case, we would write STD, oops, colon, colon see out. And what this means is inside of the IO stream library, find the namespace called STD, and then find the function called see out and use them. Thats kind of what this line means.

You dont have to understand this. Im not trying to confuse anyone, but the idea is that if we dont include this line right here, we dont know all of the stuff defined in STD. Or if I write CEO, I dont know where to look for. See out. So as soon as I include this STD, so I say using namespace STD, what that now means is that everything defined inside of STD. And Im trying to get my, see out written here, everything defined inside this namespace.

I can now access without having to reference STD before.

So as soon as I do this, anything inside of here, now I can access and use without writing STD, colon, colon CF, because now weve looked at all of this stuff inside of here and weve used it inside of this program.

So hopefully that kind of is making sense. If it doesnt make sense, you can totally just take for granted that were writing this line and well discuss it later on when we actually get into defining our own namespaces. But for now understand, its allowing us to access all of the things inside of this namespace without explicitly referencing.

If we dont use this, it doesnt mean we cant access it. It just means that we now need to write STD colon, colon, because we have to say where see out is defined.

Whereas when we have using, we are now telling the program that we want to use all the stuff inside of STD so we can use it without having to explicitly reference it. Those are the two lines, definitely the most complicated ones that I had to explain.

So sorry for the abrupt cut here. But I realized that I missed something pretty important, which is the line Terminator or the semi-colon that we see here in this program.

Now, whenever youre writing C plus plus code, youre usually going to need eight semi-colon at the end of the lung. Theres a few exceptions as youll see throughout the series. A for example, at the end of the function, you dont need a semi-colon.

Although adding one is, is totally fine. Its not gonna mess anything up with your program.

Uh, but at the end of any statement, youd write or any command.

If you want to call it that you need a Terminator or a semi-colon. Now the reason you need this is because the compiler reads kind of lines of code one at a time. So what I mean by this is we would say, this is one line of code would say, this is another line of code. Now, if I forget this Terminator whats going to happen is even if these two things are on separate lines, the compilers going to read them like this. Its going to squish them both together because theres no Terminator between this return and between this C. So thats going to cause an error. The compiler is not going to know what to do with this, because this here is not a valid command.

You cant write this, this isnt valid, but now when you add the semi-colon, this is valid because now you have two separate commands that will be interpreted as such interpreters as commands. And then they can both be executed in the correct order. So this one first, and this one after now, you dont need to have this return statement on the next line. Obviously its way easier to read, and Im going to do this in a tutorial, and this is good practice to have all of your commands or statements wherever you want to call them on separate lines. However you dont need to. You can do this. This is totally fine. In fact, even when were looking at a function like this, this is fine as well. This function will still execute.

Therell be no problem with this because C plus plus can still read all of this code because we have the proper line terminators and we have the closing and opening parentheses. So just keep that in mind. That is why we have the semi-colon and now lets resume the video.

Now lets talk about this line right here, line four. So line four, define something called a function. A function is just kind of a block of code and you can run a function and you can run it multiple times. Now we will discuss functions in depth later on, but the idea is code thats inside of these curly braces is a part of this function. So when this function runs or executes, all of the code inside of it is going to run and the code runs in sequential order. So just like line one runs first, then line two runs. First, as soon as I called the main function, the first line side of the main function runs, then the second line and so on and so forth.

And code works just as you know, like reading a book, would you read down or from top to bottom and from left to, right. So that is the idea. So this function right here though, is a little bit of a special function.

And this is whats called the entry point of our C plus plus program. So in every single C plus plus program, there is one, well call it a function. Cause Ive kind of described what that is that needs to be run immediately. As soon as the program is run. So we kind of need to tell C plus plus what code we want to run. As soon as we, you know, press enter and the console and run our compiled program. And in this case, that code that we want to run immediately must go inside of this main function. So in every single C plus plus program, we must define in the following format int space main, we then have our two parentheses. Then we have our open curly brace and our closing, closing curly brace, and anything inside of your is code that will run immediately when our program is executed. So he must have a mean function.

So we will continue in one second. I need to quickly thank the sponsor of this video and this series, which is algo expert algo expert is the best platform use and preparing for your software engineering, coding interviews. They have mock interviews, data structures, crash course over 140 coding interview practice questions, just a ton of high quality resources. So with that said, check out algo expert from the link in the description and use the code tech with Tim or a discount on the platform. Let me show you what happens if I dont define a main function in my program, or I call this something like main start. So if I do this and I go now and compound my program, so G plus plus hyphen, Oh, run program tutorial to that. CPP notice that we get an air. Now this air is very cryptic.

Its hard to read, but it says over here, undefined reference to when main at 60, what this cryptic line is telling us is we dont have a main function.

We couldnt find a reference to the main function. We need a main function. We dont have a main function.

We cant run our C plus plus program. We dont know where to start. So now if we go and we define our main function, as you know, this is going to work.

So there we go, that works. And if we run run program, now this works, uh, as we would expect. So you need a main function. Anything inside of here runs immediately.

So from now on for the rest of this tutorial, uh, all of the code needs to go inside of this main function. And later on, we will discuss how you make other functions and how you get other codes to run from this main function. But for the next few videos, just imagine all the code you write must go inside of here because its going to be executed as soon as the program is rolling. All right, hopefully this is kind of making sense, but this is the entry point of our C plus plus program. All right, lets talk about this return line. So whenever you define a function and again, well get into functions later on, you define the type that this function is going to return. You havent talked about types yet.

So I wont talk about this too much, but much functions must return a value, but is the same type that is defined that theyre going to return. So if we say this function is going to going to return it, which sends for an integer, we must return integer. So in this case we returned zero because zero is an integer and were just kind of, you know, satisfying C plus plus rules and saying, were just going to return zero to make sure we dont get an error.

Now, if you worked in other programming, languages is probably makes a ton of sense.

If youve never coded before you can somewhat ignore what Im saying, Im just explaining kind of why we need this return zero. Now, in this case, in this main function, we actually dont need this. The reason we dont need this is because this main function is the entry point of our program.

And CBOs was kind of just knows that this one can be treated a little bit differently.

We dont need this return statement, but in other functions we write, we will need this. And again, I dont want to get into this too much, cause I really dont want to confuse you guys if it doesnt make sense, ignore what I just said, if it makes sense. Great. But thats why we have this return line. This is just good practice to have in our main function. All right. So now lets talk about SIA. So see out as we saw, it takes some texts and it outputs this to the console.

Now see how it again, stands for characters output. This right here is whats called the stream insertion operator. You dont need to remember that name. You dont even really need to know exactly what it does whenever you use SIO.

You then add the stream insertion operator. And then anything after that, that you would like to insert into the stream. The stream is just the console. The text is going to be outputted to the console. Now it turns out that you can insert multiple things to the console. So you dont Jew or to the street. You dont just have to insert one thing right here. You can insert multiple.

And to do that, you would write the stream insertion operator again, and then you would write whatever else you would like to insert in this case.

Lets put tint. So now if I compile this, so lets compile this program and lets run it, we see we get hello world. Then we get Tim with no space. So that is how we add multiple things to the stream.

Now lets see what happens if we use see out a second time. So now I dont want to just output one line. I want to output multiple lines of text, right? And ideally let lets change this to like Joe or something. We want this line to run first and then this line. So lets just run this and see what we get. So G plus plus hyphen O run programs, tutorial to run program. Now notice we get hello world exclamation point, Tim.

Hello, world, exclamation what Joe.

But look, these were not on a separate line.

Now, intuitively you would think that these would just go on two separate lines, right? The reason they dont go on two separate lines is because see out is just accessing the console and just shooting characters onto the console. And we need to tell CEO to go to the next line. So to go to the next line, we need to use something called an escape character.

Now, whenever you actually working in any like word document or console or any, anything where youre writing texts, these is little invisible characters called escape characters. Now theyre defined by a backslash and then some character or sequence of characters that kind of tell the console what to do with this.

So backslash N is what I believe is called the carriage return. And whenever you press enter, what youre really doing is youre inserting an invisible backslash and into the console that kind of tells the console to go down to the next lunch.

Now thats a little bit of a line, but you can kind of think of it that way. So when I have this backslash N this tells my console to go to the next line.

So if I add my backslash and here at the end of this CEO, what you will see is after the CEO, the console will go to the next line and then it will print this line right here. So let me just show you. So if I do this G plus plus run program and run program dot exe, we see that the next line shows up afterwards, right? Because we added this carriage return, this backslash. And so lets out another one. And lets see what happens when we have two backslash ends just for experimental purposes.

So we will compile and oops, lets go here. We will now run and notice we get whole world blank line and then hello world again, because we went down to lines. So that is backslash. And that is how you get down to the next line. Now theres some more escape characters that we can use. And escape characters is anything that starts with a backslash. I wont get into them right now, but youre welcome to look them up and you can kind of mess with them in the console and see exactly what they do, but thats the idea behind backslash. Now it turns out that theres another way to do this. Theres a little bit cleaner that doesnt require us to use a backslash. I might mess this up cause I forget exactly what it is, but I believe its called end L now end L is just a nicer, cleaner way to read something. So what Im going to do is Im going to add this end L to my stream. So Im going to insert it after Tim. And what this is going to do is now Telsey out to go to the next line. And L is really just an escape character. Its really backslash. And it just kind of hiding that as backslash. And so that if youre reading this, its easier to understand this means go to the next line. So now when I add and L to the end of my stream here, and I go ahead and compile my program, so lets just clear the screen to give us some space.

Lets compile, lets run. Notice it goes to the next line and I could add another and L so I would do this and then end L like that.

And now lets see what happens if we go to the next line. So now we go to ones, right? So thats the idea behind SIOP. So let me zoom out here. So you can see everything that is NL. That is the escape characters. And honestly, I think thats all I really needed to discuss in this video. So let me just quickly recap some of this stuff. As you can see, were reading line by line, we start in this main function, right? And then we read the first line in the main function, the second line, the main function, the third line in the main function and so on and so forth now see out does not automatically print something on another line. So you need to make sure youre explicitly saying, if you want to go to the next line by using that backslash and or that end L that I just discussed right here. Now well talk about why some of this is in strings, why some, this is not in strings. And when I say string string, Im talking about quotation marks. Im getting a little bit ahead of myself in the next video, but yeah, thats all I really need to discuss in this video.

So with that said, hopefully this is clear. Hopefully this gave you an idea of how he C plus plus program actually works. And the key takeaway here is that this main function is the entry point of our C plus plus program. And that we need to include functionality that were going to be using in our C plus plus program, because by default, it is not all here. So that said, I hope you enjoyed. If you did make sure you pay, like subscribe to the channel and I will see you in another YouTube.

Tech With Tim: Learn C++ With Me #2 - C++ Program Structure - Software Development