Beginners Guide to C++ Programming

By Jelena D

May 23, 2018 Programming No comments

Before we get into the code we are going to go over a basic tool, a variable. If you do not know what a variable is then you’re in big trouble! Just kidding, I will explain what a variable is. If you remember math class in school then you should have an idea of what a variable is. Possibly the most used and popular variable ever used in math is the letter X. Ah, brings back memories doesn’t it? Look at the below equation:

x = 2 + 2

The answer to this equation should be apparent but it is still represented by the letter x. It is basically a ‘holding’ spot for when you want to write an equation down and your really do not know the answer to. Say for instance we change the above equation to the following:

456 / 5 * 6 + 99 *46 / .09 = ?

Now the answer to that equation isn’t very evident. Mixing grammar and math is bad practice and they do not go well together. Instead of using the ? symbol you would use the letter X. Regardless if you know the answer to the above question or not, you do know that the answer IS x, whatever x may be. Variables in math work similar to variables in almost any programming language. The best part of using variables in a programming language is that they can be made up of whatever you can think up. However there are a few rules you have to follow.

Beginners Guide to C++ Programming

Has to start with a letter

Can not use reserved keywords (we will get into this later)

Can not use mathematical or foreign characters (nothing like ¿ ( á + -↓+? )

Can not be the same as a previous variable.

If you abide by those rules you can name your variables anything you want. Be careful though, loosing track of which variables belong to which ‘equation’ could be a problem. You typically want to name your variables something that will remind you of what it was for. For example, you can use the following equation just fine, x = 2+2. But really that isn’t the best way to do it. It would be a lot simpler for everyone if you used the word ‘sum’ instead of ‘x’. Try to keep variables named something that resembles what you are trying to do with it. This is especially helpful when you find yourself using quite a bit of variables, if you just give all your variables random names you may have problems finding which ones you need to reuse.

Declaring a variable

Declaring a variable is the process of informing your compiler that you are going to assign space, on your computer RAM, for that variable. Different variable types will take up different amounts of memory; it varies on different computers as well. Before we get into declaring a variable we will need to decide on a variable type.

A variable type can be anything from an integer to a character. The C++ programming language offers a wide range of variable types. We will not go into them all, some of them are pretty complex and there are just too many to go over. The two types we will cover for now are integers and characters. But before we go over them we need to talk briefly about keywords.

Keywords are words that are reserved by the C++ language. These keywords define certain parts of the language. You can not use these keywords as variable names. You can easily tell when you use a keyword because all compilers will change them to bold text. The keyword for an integer is int and the keyword for a character is char. Most compilers do not allow you to format text in anyway so you will not be able to make any words in bold, the compiler will automatically change them to bold text.

When you declare a variable you will first need to list its type and then whatever words or letters you want to use. For example…

int sum;

The above is how you will declare all of your integer variables. Take special note of the semi-colon after the declaration. Just about every line of code you use will end with a semi-colon. This informs the compiler that you are finished with that specific line of code. Now that you know how to declare a variable, let’s go into the basic code setup. This basic code setup is what you will typically use when creating small programs. Go ahead and enter the following into a new source code document in your compiler. In most basic compliers opening a new source code document is as easy as going to File gt; New gt; Source Code Document. It will vary depending on which compiler you are using.

using namespace std;
int main()
cout lt; lt;”Hello World”;
return 0;

When you are done just hit compile and run, or something similar, and you should see a command prompt come up with the words “Hello World” printed to it. This is the most basic starting point for learning to program in C++. In the next article we will go over what all that code above means after we talk shortly about what a function is and how it works.