Site hosted by Angelfire.com: Build your free website today!

Introduction to C++


First off, the first thing you need is a good code editor and compiler. My editor of choice is Dev-C++. A link can also be found on the software page. Download and install Dev-C++.
Once this is done, go ahead and take a look around. The first thing you should do is create a new source file from the file menu. The code generated should look something like this:
#include <iostream.h>
#include <stdlib.h>

int main()
{

      system("PAUSE");
      return 0;
}

Let me explain what is going on here. For our intents and purposes system("PAUSE"); is not needed. You may want to go ahead and delete that line. The statement #include <iostream.h> is what is known as a preprocessor directive. Basically what happens here is that another file in this case iostream.h is included in the compilation of the source code. iostream.h allows your program to access standard output and input (keyboard and screen). Most programs will want this included.

int main() is called a function definition. The first piece of information int lets us know that the function will return an integer to the place it was called from. In this case the function is named main. All programs must have exactly one function named main. The operating system calls this function when the program is run, therefore, if it doesn't exist, then nothing gets done.

Some basic definitions are needed now. An int is a piece of data that is an integer. An integer is a whole number, such as 1,2,5,-1 etc. ; is the end of statement operator. When the compiler comes across a ; it knows that the statement preceeding the semi-colon has now ended.

return 0; signifies the end of the main function, thereby signifying the end of the program.

Let's write your first C++ program now. Use the new source code file we have already made. Go ahead and save it now as "hello.cpp".

Go ahead and delete the system("PAUSE"); line, we won't be using it. We are basically going to write a program that says "Hello World!" to the user. To accomplish this we are going to use the standard output method called cout.

To use the cout operation we must call it like this: cout << "Hello World!" << endl;

The operators << act sort of like arrows pointing the data to the correct place, which in the case is the output (cout). << operators must be used in between different pieces of data. endl tells the output to skip to the next line. It stands for End Line.

We are going to call the cout method in the function main() right before return 0;

The code should now look something like this:
#include <iostream.h>
#include <stdlib.h>

int main()
{
   
   cout << "Hello World!" << endl;
   return 0;

}


Go ahead and compile the program and then open a DOS prompt and run it. Congratulations! You have just written a C++ program.

Back to C++ Tutorials