Header Ads

Getting Started - Lecture 3 - C++ Language for Beginners

Learn C++ Language
Lecture # 03Getting Started
Learn C++ Language
Please wait a little bit to Load

So if you open up code blocks you're going to want to click on Create a new project and then click on console application on mine it's in the top right corner of the window on yours it may be different but you want to click on console application and then hit go then on the next window you want to hit next until you get here.
Make sure you highlight C++ click Next again and then give your project a name and man I'm just going to call it Tauriel wind and then specify a folder to keep it in. Make sure it's a folder that you can find easily and then click next. Leave all the default settings on this window right here. These are simply directory and compiler settings.
Just hit thinnish and then your project is created.
So right now you don't see anything but if you go over here to the left and click on sources you'll see the main does the file which stands for main dot C plus plus. And if you double click it you'll see the code. Now before we analyze this code I want to go ahead and show you how to run it and what this code does and to do that on Windows you can either hit F 9 or if you're on a Mac or Linux machine you can just go up here and hit build and build and run.
The code will compile and then you'll see this console window.
It prints the word hello world and then says Process returned 0.
OK so you can close up of this.
Now that we know what the code does is we're gonna look at how this code is what it is. So beginning with kind of the main thing I want to show you in this tutorial other than you know how to run your first program and compile it is you know how to type out the skeleton of a C++ program.
It's what I call the skeleton and it's everything that you need for your code to run at least in this course any ways for every program that we will be writing together.
So if we go ahead I want to go ahead and take out this line because this line is not actually needed for this program to compile and run.
So if we take it out and we hit F 9 and we build and run it again we will get the processor turned to zero. We just won't have hello world printed to the console which means I mean that's fine.
The program just ran and executed till it finished with no errors.
And it's a perfectly good program. So if we exit out of this we're going t o now analyze everything that we need which is everything that you see right here. So starting from the top you're going to see pounde include IO stream this lot of code right here just simply tells the program that it needs to include a C++ library known as i o stream which stands for input output stream.

Now on every program that we write you will need this line of code which is why I include it in our skeleton is because you know if you don't have this line of code your program will lose its basic input and output functionality. So you do need this line of code and every program that we write. Moving on you're going to see using namespace standard. Now this line of code is not necessary for your program to run.


OK so if we took this code out right now it should still run fine. We hit F NAND. Everything goes fine and we still get the same result process returns 0. However I do want this line of code in here for reasons that I'll explain in the future.
For now just know that we do want to include it as part of our skeleton because it will make your life easier when we start writing more code and I will show you on future tutorials.
But for now just know that you do need using namespace standard semi-colon.
OK.
And it does it with the semi-colon on. Right now you're saying why does this line of code end with a semi-colon and this one doesn't. Well we'll get to that in future. Toils again.
It's all going to become a habit. For now just know this. This is the code that you will need in all of our C++ programs. Moving on to the next big chunk of code. This right here is known as your main function and in every C++ program that we write you will need a main function and type it up. You just simply write I continue which stands for integer and then main open print close parentheses and then your brackets with the return 0 statement.
Now in in programming there's two different conventions for writing these brackets I want to go ahead and show them to you now so you don't get confused. Later if I do this one convention is the way I just saw it which is like this where your brackets are open and closed down here. But the other way you may see it is like this where your brackets open at the top and close down there which is fine there is no difference in the code whatsoever it will run just the same. So I just know that it's it's just a programming convention.
There's no right or wrong way. Some people have their own opinions on why they do it a certain way but just know you know it's all a matter of preference. So before we end this tutorial on when you go through and I want you to top this up with me so you can get in the habit of doing it. So what's the first thing we need to do.
What we need to include the input output string C plus plus libraries so we can you know output stuff to the console window to do that we're going to hit pounde include IO stream. Ok no semi-colon on this line again.
He'll get in the habit of no where to put him where not to put him. But for now you know just no there's no semi-colon at the end of this line. Now though we don't need it for this program. You do one is part of your skeleton for this class Id want you to get in the habit of writing it and all of our programs.
So let's go ahead and do it now. You want to use the standard namespace.
And how do we do that. Remember we typed using namespace standard and that one did have a semi-colon moving on the other really critical piece of code that we need for our program to run is the main function. And remember that was preceded with IMT which did for integer. The name of the function main open Princie is closed parentheses and then our brackets depending on what convention you decide to use will be different.
And then return the value 0. This right here is working code. If you take out some of this code sections that this code or return an error it will not run. So for now just know that everything in this code is needed and in future tutorials will go into a discussion as to why they are needed and what exactly they do.
Before we get into the code blocks editor I kind of want to do this on a notepad sheet real quick so that we can we can show you I want to kind of discuss with you how these data types are declared and what they mean. So a datatype what is a data type. What is a data type a datatype is basically a description of what we are using. So for instance and the real world if we were going to use the this the letter B for instance. Well this is to us is known as a letter. This would be the datatype in programming. OK. Or what if we were talking about the number 7 in our world. This is called a number. This again is a data type.
And you know if we were talking about programming except in programming and in C plus plus we don't call them letters numbers. So how what do we call them. Well let's start with just a single letter. Let's let's just start with the character B. OK. Again this is a letter but in programming. This is called a character data type and the character data type. Sorry about that. The character data type is denoted or kind of encapsulated with single quotation marks. So.
So that's how. So. And C plus plus this letter B It's called character data and you have to declare it with single quotation marks. And when we get into example code you or you'll understand what I mean by declarer with single quotation marks.
But let's move on to a number. So let's say 7 in C plus plus This is called an integer and an integer is denoted just as it's seen. No quotation marks nothing special. What about multiple letters. You know what about you know the name Bill. You know that's four characters but this thing is a whole what is it. Is that a word you know in in our world it's called a word. But what is it. What is it in C plus plus well and C plus plus it is known as a string which is alpha numeric data. OK but for now just now it's called a string data and you denote stream data with double quotation marks as so.
OK.
And I kind of want to go over one last thing. And that's decimal numbers so like seven point seven seven you know. Is that an integer. Well no it's not. It's not a whole number. So what is that called.
Well in this it can be one of two things you know actually can be multiple things. But for this course we're going to keep it simple know that it's either a flow or a double. OK. And it's denoted just as seven point seven seven. For this class we're going to use the word double And the reason why is because if you use the word flow when you declare it as this seven point seven seven behind the scenes in code blocks code blocks automatically and convert it to a double anyways.
So for now we're just going to call it a double data type. OK so let's move on to some actual code. OK. And we're going to practice declaring these four major data types that I've shown you here. Let's open up our code.
And by the way the last tutorial the code was I zoomed in if you couldn't tell if it was hard for you to read in the last tutorial. Hopefully this will make it easier for you to read. So right now let's go ahead and practice what we learned in the last tutorial in this project. I want to practice writing our skeleton so to speak everything we need for our code right. So we need to do basic input output library.
We need to include it. So let's include IO stream. OK we want to use the standard namespace coding. And then we need to declare our main function and we need a return value for this main function which as we said we're going to use 0 and this is our skeleton This is everything we learned in the last tutorial. Again if he if this isn't if you still haven't got this down yet you know as I suggest because practicing it over and over until you get it down to where you can basically do write this code in your sleep to get your program to compile and Ryan as with process returns zero.
Let's get into our datatypes and variables. OK. So we discussed how to do what the data types were you know it's kind of a description of what you're dealing with but what's a variable. Well a variable is kind of like a box.
And your data type is a label on that box. That's how when you picture this you have a box and then you have a label on this box. OK so. So let's say we put the letter B inside a box.
So the letter B is character data right. We discussed this earlier. So to declare the character data we write C A R which stands for character. OK. So that's our label. Character c ha our that we're putting on our box. Now our box is user defined meaning we can name it whatever we want. So I'm going to name it letter because that's what's in this box is a letter.
It's a letter. OK. And then we need to declare what the letter is. We put a equal sign. I'm sorry about that guys I don't know what that is. Pop in to say put that equal sign. OK.
And then again character data is denoted with single quotation marks and then inside the single quotation marks we put our letter B and then end the statement with a semi-colon. And this right here is declaring a variable with the data type of character and that variable that character variable is the letter B. OK so let's move on. Let's let's let's move on to the other. Now if you don't know what this is used for yet that's fine. In the next tutorial we're going to discuss how we use these variables and what exactly they're used for.
But for now we're just declaring them and I'm kind of giving you a visualization of what they are. OK so just for now think of them as a a box with a label and then something inside the box. In this case it's a box that holds characters and in the box is the character B. OK.
So let's move on to a number of box holds numbers. So in the end a number is going to be an integer. If it's a whole number. So to declare a whole number integer value we write the word I.A. which she makes you probably remember from here.
Maine which we'll get into. Why do you need that again in future tutorials. But for now let's focus on this and we're going to put a number in here so let's call this variable number again it's user defined. If I wanted to call it Jimmy I could call it Jimmy.
But you kind of want it you know it's convention to call it something they represent. So we're going to call it a number. And that's going to equal the number seven. And you end that with a semi-colon. Remember no quotation marks right here just the number. OK. That's that's another variable.
Let's do the other two variables. We did let's do a let's make a variable that holds the name Bill inside. So to do that we write suring because that's the data type remember multiple letters is Alpha alphanumeric data is called string data. OK. Which you know I'll get into it later string. It's technically a class and I know you don't know what class is over now I just know it's it is a data type for now. OK. String data.
And we're going to call it name equals double quotation marks. Bill semi-colon OK so there's your variable for a name or multi character data. OK. Now and in string don't don't get it confused with only being characters for my to do. Bill '99 hyphen hyphen hyphen semicolon semi-colon inside quotation marks. That's fine alphanumeric data will hold all these characters and this will not cause an error. This is everything you can keep in this variable that is fine.
So I just know that. But for now actually let's call it Bill '99 OK so that you don't forget that you can actually put numbers in string if you want to do just 99 you could design's you have to double quotation marks it's still string data. OK.
But for now it's Bill 99. And then for the last data type Let's do a decimal number. Rich remember I said it could be float or double but for this class we're going to use double. So it's a double let's call it decimal equals seven point seven seven semi-colon. And all this is fine if we run this program it will not cause any errors. Everything runs processor turns zero.
Same outcome. All this stuff is happening behind the scenes of course you won't see anything in the console window when we run it. Anything different you know you'll still get the same result process returns 0. Everything's fine.
But the point is to just show you how to declare these variables and data types to discuss what they are and show that you know if you if you declare them right you won't get an error. Let me show you what will happen if you if you know if you call this character or let's say we call it stirring. OK. Without the double quotation marks.
If you declare it wrong which this is declared wrong because you're calling it a string and you don't have quotation marks if you want it to be right you'd have to put in double quotation marks. But if you take those out and this is declared wrong and when you try to build and run you'll get an error. See this red box and if you look down here in the log you can scroll down and it says air conversion from double to non scalar type standard calling Kolins string requested. And that build failed one layer.
So that right there will cause the program to crash. So when you change that back to double and if we run it again build in around everything will go fine. So that's it for this tutorial.
Let's move on to the next tutorial.

No comments

Thanks for Your Precious Words :)