A little C++ starter

So I recently had some questions about learning C++. This post attempts to get people started on this language. I will start in no particular order other than what seems like a good idea at the time, so if you have been coding for a while, this won’t add much. If you are just wanting to try it out, hopefully this will explain a few things. Good luck L!

To start off, check out the website http://cpp.sh/. It will allow you to paste this code in there and see what it does. Lets start off by explaining the sample code that it gives you when you start off.

VARIABLES
Lets start off by talking about variables. a variable is a thing that the computer uses to remember something. You can picture it like a box with a name tag on it. Different types of variables can hold different things. Some boxes can hold numbers. Other boxes hold words. In C++, the type of variable that holds words is called a string. You can create a variable by just telling the computer its type (which in this case is a std::string) and then giving it a name (which, funny enough, on line 7, its name is really just ‘name’). For this post, we’ll just use strings. I’ll talk about other types of variables in a future post. The important thing to remember though, is that if you want the computer to remember something for later, you store it in a variable that you make.

FUNCTIONS
An easy way of thinking about functions, is to think of them as just a group of commands that you are telling the computer. You can then give this group of commands a name. With some functions, you might have to give it more information so you pass in parameters to it. Think of it like this: If I had a function that painted a house, I might call it PaintHouse(). But if I wanted to always be able to choose a color of the house, I might want to say PaintHouse(blue); or PaintHouse(red);. The function is the same (it still paints the house) but it now it knows what color.

Let’s continue by looking at the program line by line. With C++, anything that is after “//” is a comment. The computer just ignores it. You can see that on line 1, that whole line is ignored. The next 2 lines are #include lines. they tell the computer to read some code that was already written, and allow you to use it. Great! less work for us! In this case, we want the computer to be able to use the screen and keyboard, so we #include <iostream>. In the next line, we #include <string> so that the computer knows about those strings that I wrote about earlier.

Now we get to the interesting parts! When you write a program, the program has to start somewhere. In C++, one way of telling the computer where to start is by specifying the function main(). You can see this on line 5. After you declare a function, all of the code that is between the { and } is a part of it.

On line 7 we declare a variable called “name”. Notice that at the end of each command in C++, we put a semicolon. This is important! It tells the computer that the command is complete and to start looking for the next one. On line 8, we want to send some stuff to the screen. to do that, we use a variable called cout. That means “console out”. Where did this come from? It is there because of line 2 where we #include <iostream>. So if we have a string after the << it will be displayed on the screen. Next, we have a function that was already written for us called getline. This reads in letters from somewhere and stores them in a variable. Where does it get them? It gets them from cin (which we pass into the function. cin means “console in”, which is the keyboard! Where does it put what you type? It puts it in the variable name. That is the second parameter that is on that line. Finally, on line 10, we use cout again. This time though, we send it 3 strings! The first one is “Hello, “. The next is a variable that holds whatever you typed. and finally, it sends an exclamation point and a newline character. (That just says to start a new line. It’s like the computer is pressing enter).

I hope this very basic introduction helps you understand what is going on in this sample program! Go to the http://cpp.sh website and try it out. Then try changing what it says. The best way to learn is to just experiment with it, and keep trying new things. Good luck!

Also, if you are an experienced developer, you will notice that I glossed over many areas. This article is intended to keep things as simple as possible for the true beginner, so please forgive me.

Comments are closed.