Advertise here.

Sunday 19 August 2012

C++ Programming Tutorial Lesson #1 - First Program


Requirements

Before we start programming in C++ we need a compiler. We will be using a command line compiler for this tutorial. I will be using Borland C++ Compiler 5.5 but you can use any other ANSI/ISO compliant compiler such as gcc.

Hello World Program

Our first C++ program will print the message "Hello World" on the screen. Open a text editor and start by typing the following line:
#include<iostream>
The above line includes the header file called iostream which will allow us to use the command to print words on the screen. Next you must type:
using namespace std;
This will let us use certain commands without having to type out their full name. Now we will type the main function.
int main()
{
}
The main function is where a program always starts. Every program must have a main function. The word int in front of main is to say what the return value is. The curly brackets belong to the main function and show where it begins and where it ends. Now we will type the command that prints "Hello World" on the screen between the curly brackets.
cout << "Hello World\n";
The cout command is used to print things on the screen. The << means that the text must be output. The words that you want printed must be surrounded by quotes. The \n means that the cursor must go the beginning of the next line. Lastly we must return 0 to the operating system to tell it that there were no errors while running the program.
return 0;
The full program should look like this:
#include<iostream>
using namespace std;

int main()
{
   cout << "Hello World\n";
   return 0;
}
Save the file as hello.cpp. You now need to compile the program. You need to open a command prompt and type the command name of your C++ compiler and the name of the C++ file you have just created. Here is an example of how to do it with Borland C++:
bcc32 hello.cpp
If you are given error messages then you have made mistakes which means you should go through this lesson again and fix them. If you don't get any errors then you should end up with an executable file which in my case is called hello.exe. Enter hello to run the program and you should see "Hello World" printed on the screen. Congratulations! You have just made your first C++ program.
Important note for Borland C++ users
Once you have downloaded and installed Borland C++ you need to add "C:\Borland\BCC55\Bin" to your path. If you are using Windows NT/2000/XP then you need to open the control panel and then double click on "system". Click on the "Advanced" tab and then click "Environment Variables". Select the item called "Path" and then click "Edit". Add ";C:\Borland\BCC55\Bin" to "Variable value" in the dialog box that appears and then click Ok and close everything. You will also have to create a text file called "bcc32.cfg" in "C:\Borland\BCC55\Bin" and save the following lines of text in it:
-I"C:\Borland\BCC55\include"
-L"C:\Borland\BCC55\lib"

Comments

Comments explain what a program does and are ignored by the compiler. A single line comment goes after a // and a multiple line comment goes between a /* and a */. Here is an example of how to comment the program we have just made:
/* This program will print the words
Hello World on the screen. */

#include<iostream>
using namespace std;

int main()
{
   cout << "Hello World\n"; // Print Hello World on the screen
   return 0; // Return 0 to the OS
}