Saturday, December 24, 2011

C++ Tutorial - 1

Welcome!
Welcome to AlbinoShadow's C++ Tutorial Series, where I will teach you C++ one step at a time! C++ is an OOP language, or an object-oriented programming language. This means that it uses data structures, etc... Basically it uses "objects". In this thread I will be teaching you through tutorials and explanation, on how to use the C++ programming language. I will update this every now and then with new tutorials and possibly make this into a more common thing if people enjoy them.

Where is C++ Used?
C++ is used in many commercialized software products, ranging from famous GFX products, to some of the biggest hit games, such as World of Warcraft and Call of Duty. C++ is used to develop many game engines, as it is normally the root of a project. Colleges use C++ in their programming courses sometimes too, this is changing over to Java, but businesses are still using C++ to make programs so I would recommend knowing C++ and Java both as they are the two most used languages.

Integrated Development Environments
IDEs or Integrated Development Environments are sort of like the notepad for coders. An IDE is used to create/edit/run codes, it is like the toolbox for a plumber; without it, there isn't much he can do. In most IDEs there are the following;
-Source Code Editor, this is used to edit your programs "scripts". All IDEs should have this, as it is needed to create prorgams.
-Compiler, compilers are used for running your code and translating it into binary. For without a compiler you are just trying to make a program out of text... Not very helpful, amirite?
-Debugger, as most IDEs have a debugger, these will help you go through your code to find errors. Without a debugger you might be running a piece of broken code which would just crash the program! We don't want that do we?

So now we need an IDE to begin developing our programs. I recommend using either Visual Studio, Visual C++, or Code::Blocks. I will supply you with the links for all three, but for these tutorials I will be switching back and forth in between Visual Studio and Visual C++. Both have a very familiar graphical interface so either will work. If you are going to use Visual Studio which is probably the best of the three, you must either pay the price or become a pirate...
Here are the download links for all three;

I suggest the Visual products for Windows users and then Code::Blocks for an alternative for Windows or as a Linux/Mac coder... If you are on Linux you may use GCC and if you are on Mac you may use Xcode.

Getting Started
For this tutorial I will be using the Visual products. Visual Studio is extremely similar to Visual C++ so either one of those will work for starting up your first project.
First thing you are going to want to do is open the program of course. From there hit 'New Project' and you will be greeted with a screen like so;

You should hit Empty Project, I might go into detail what the other projects are for at a different time. I named mine HFC++Tutorial1, you may name it the same or differently if you would like. You should be greeted by an empty screen with a window called the 'Solution Explorer' on the side; this is the programs files you are working with. Currently it is empty besides some of the External Dependencies. Don't worry about those right now, as that is for more advanced coding projects. You need to add some code right now to make things happen inside this IDE, so right click on 'Source Files', go over 'Add', and hit 'New Item'. Here's a picture for the picture friendly; http://i.imgur.com/aUIIV.png

You will be greeted by yet ANOTHER screen (these guys are a lot of fun), and for this you will be selecting a cpp file and you should name it 'main'. No capitalization or anything. This essentially is the heart of your program. http://i.imgur.com/1Ti0a.png

Hit add and you will be shown a white page, this is the source code of your program. Now it's time to add some code. The first three lines of your code will be your declarations.
#include <iostream> //This is including the library "iostream", it used to input and output through the stream

using namespace std; //This code should be avoided if you ever code professionally
//It is used sort of as a shortcut so later in our code we do not have to
//type as much.
Copy and paste that code into your source code editor and you will have this: http://i.imgur.com/NbFyk.png

Now lets explain those two written lines of code. "#include <iostream>" is including the iostream library of C++, a library used for sending input and output in between the user and the program. Next line is "using namespace std;" now lets break this line down. Using namespace std basically means that you are going to be including all of the standard library files, this includes some things we will be using later in the program. You must include the semi-colon at the end (;) as it tells the program to go to the next line.
Next we will begin to write our program. We will be creating the popular "Hello world!" program. I am going to give you the full program and explain it all in-depth. Add the follow code after the previous lines I had given.
int main()
{
cout << "Hello World!"; //Displaying the information in the console.
cin.get(); //Require a key to be pressed to go on.
return 0; //Returns the program with a 0 value.
}
Your program should like this now: http://i.imgur.com/6KD32.png

Okay now let me explain the following the program. Int main() is basically declaring the start of the program, it lets the compiler know where it is starting, this is required in EVERY program. After int main() comes a squiggly bracket "{", these too are required in every program. The squiggly bracket lets the compiler know what is inside the program, these will be your best friends and also your worst enemies later. Many errors when compiling are caused because you are missing a closing bracket. At the end of the program we have a squiggly bracket too, this is showing the compiler the end of the program.
Next part of the program is the 'cout << "Hello World!";', this is a line of code that refers to the line 'using namespace std;' as cout is a C++ standard library function. Another way to write it is std::cout, but that is for a different lesson. Next we use the << operand to show what information we are going to output to the console (cout is like saying console output). "Hello World!" is the information we will output. Of course you can edit inside the quote for different responses. Don't forget the semi-colon though, as that will inform the program to go to the next step!
After cout comes cin.get(), this prevents the program from continuing until the user gives input through the enter key. There are other methods to do this but cin.get() is the "best" because some other methods create unhealthy habits. So, basically you will use the cin.get() to prevent the programming from continuing until the enter button is pressed. Again, don't forget your semi-colon.
Next is return 0;, this is not needed in C++ I don't believe, but I added it anyways. C requires it C++ does not (correct me if I am wrong), but it basically returns a value to your program, I cannot explain it very well.

Running Your Program
To run your program once you have finished you may either use the short-cut which is F5 or press the green arrow I refer to in this handy-dandy picture.

If you enjoyed this tutorial leave some feedback! Whether it is positive or constructive criticism I will read it all and base the future series on it!

Introduction

Hello fellow computer users,
I will be taking advantage of BlogSpot to teach various things, such as C++, Java, and other computer related topics. If you are interested in learning any of these various things, please follow me. The blog will be updated throughout each day and I will also be taking requests in the comments to what I should be teaching. I plan on starting with teaching the basics or a coding language. If you would like to contact me please send me an email at justpwnedanoob@gmail.com. Thanks, and I hope to see you following some of these tutorials!