Sunday, January 20, 2008

Intro To C++!

Tutorials created by Fuse

Promoted Sites:
www.NextGenBoards.com



Tools used for this tutorial-



Tutorial Introduction-

I will be writing a series of c++ tutorials over the next few months. I am going to teach the basics (console programming) to the more advanced (including winapi, and directx). I have been studying c++ on and off for about 4 years now, recently i have been reading allot of directx tutorials and I'm planning on completing my series of c++ tutorials with a complete directory of tutorials with everything you need to know to make a full direct3d game. I'm hoping to add some 3d modeling and game audio tutorials as well but that will come after the programming tutorials. Hopefully i can get this whole project done within 6 months, its definitely possible. I hope you enjoy and take in what i am trying to teach, it will take allot of dedication and practise to be able to program on your own two feet, but i will try to make learning the c++ language as easy as possible. Lets get started. If you have not already, download Visual Studio 2008 Express, i have provided a link above.

After downloading install the program to its default directory.

When installation is complete, open up Visual C++ 2008 Express. If you have any problems installing or downloading just post a comment and i will reply asap.


Making a new project in visual C++:

If you have been following along visual c++ should already be open, if not go ahead and open it. To make a new project go to File>New>Project. The new project window will open, we need to make a "Win32 Console Application" so make sure you click it. Before we can move on we need to name our project, for the sake of this tutorial we will call it helloworld.



Click OK, then click next. In the application settings under application type click the bubble for "Console Application". Then under the "Additional Options" check the "Empty project" box.



This will be the default settings that you will use to make a console application, go ahead and click finish.

We only have one more step before we are done setting up the project, we need to add a source file to our project. To do this Right click on the source files folder then click add>new item. The source files folder can be found under the solution explorer which is located on the left side of the screen. If you do not see it hold down Ctrl+Alt+L to open it up.




The "Add New Item" window will pop up, we need to add a new C++ Source File (.cpp file) so click the C++ Source File and then name your source file, you can name it whatever you want, i went ahead and named mine hellosource.



Click add, you will probably notice that a new tab has opened up named hellosource.cpp that contains a editor, this is where we will program our actual console application.




We are now ready to start programming!



Your First Program!:
At this point you are probably wondering what a console application is. Ill try to explain it as simple as i can, a console application is a computer program designed to be used via a text-only computer interface. Dos is a type of console application. Lets go ahead and get started on some programming! Im going to go ahead and type out the source code, and explain each part in detail afterwards.

//Include the iostream standard file
#include <iostream>

//Include the standard namespace library
using namespace std;

//Beginning of the main function
int main ()
{
//Prints Hello World! to the screen and
//starts a new line using endl;
cout << "Hello World!" << endl;

//Pauses the main loop, and resumes with user input
system("Pause");

//return the value of zero
return 0;
}
To compile and run this program copy and paste all of the above code into the visual c++ editor exactly as shown. Goto the debug menu and click "Start Debugging" or you can just press F5 on your keyboard. You should end up with something like this.

Now lets learn what each part of the code does.

Descriptions from cplusplus!

// This is a comment line. All lines beginning with two slash signs (//) are considered comments and do not have any effect on the behavior of the program. The programmer can use them to include short explanations or observations within the source code itself. In this case, the line is a brief description of what our program is.


#include <iostream>
Lines beginning with a pound sign (#) are directives for the preprocessor. They are not regular code lines with expressions but indications for the compiler's preprocessor. In this case the directive #include <iostream> tells the preprocessor to include the iostream standard file. This specific file (iostream) includes the declarations of the basic standard input-output library in C++, and it is included because its functionality is going to be used later in the program.

using namespace std; All the elements of the standard C++ library are declared within what is called a namespace, the namespace with the name std. So in order to access its functionality we declare with this expression that we will be using these entities. This line is very frequent in C++ programs that use the standard library, and in fact it will be included in most of the source codes included in these tutorials.


int main () This line corresponds to the beginning of the definition of the main function. The main function is the point by where all C++ programs start their execution, independently of its location within the source code. It does not matter whether there are other functions with other names defined before or after it - the instructions contained within this function's definition will always be the first ones to be executed in any C++ program. For that same reason, it is essential that all C++ programs have a main function.
The word main is followed in the code by a pair of parentheses (()). That is because it is a function declaration: In C++, what differentiates a function declaration from other types of expressions are these parentheses that follow its name. Optionally, these parentheses may enclose a list of parameters within them.


Right after these parentheses we can find the body of the main function enclosed in braces ({}). What is contained within these braces is what the function does when it is executed.

cout << "Hello World" << endl; This line is a C++ statement. A statement is a simple or compound expression that can actually produce some effect. In fact, this statement performs the only action that generates a visible effect in our first program.

cout represents the standard output stream in C++, and the meaning of the entire statement is to insert a sequence of characters (in this case the Hello World sequence of characters) into the standard output stream (which usually is the screen).

cout is declared in the iostream standard file within the std namespace, so that's why we needed to include that specific file and to declare that we were going to use this specific namespace earlier in our code.


<< endl is used to end the line and start a new one, it is used mainly for formatting reasons.


Notice that the statement ends with a semicolon character (;). This character is used to mark the end of the statement and in fact it must be included at the end of all expression statements in all C++ programs (one of the most common syntax errors is indeed to forget to include some semicolon after a statement).


return 0; The return statement causes the main function to finish. return may be followed by a return code (in our example is followed by the return code 0). A return code of 0 for the main function is generally interpreted as the program worked as expected without any errors during its execution. This is the most usual way to end a C++ console program.

Lesson Review:

Congratulations you are now a c++ programmer, lets review what we just learned.

  • How to make a new visual c++ project
  • How to add a source code to the project
  • How to make a basic hello world console application
  • In depth explanations of each part of the hello world program


Practice Exercise:

C++ takes alot of practice so i will have a exercise at the end of each tutorial, i will try to make the exercises as interesting as possible.

Exercise: Mess around with the cout statement, try to make a program display a small paragraph. The most important thing is to get a feel for programming, stay tuned for my next tutorial, and if there any questions or suggestions then just leave me a comment.