Home / Language / English / C++ tutorial

C++ tutorial

Learn about C++

Reference: http://cplus.about.com/od/learning1/ss/clessonone.htm

Before You Start

The C++ tutorial is suitable for anyone even if you haven’t programmed before but you must have access to a computer and be able to do the following before you start the tutorial.

·         Create and edit text files.

·         Download and Install a compiler, from the links provided.

·         Run the chosen compiler (see list below) and compile and link files from the tutorial instructions.

Suitable Compilers

If you don’t have a C++ compiler already then there are many suitable free compilers. In fact most are suitable but these three are the ones we suggest as they are all free.

·         Borland Turbo C++ Explorer.

·         Microsoft Visual C++ 2005 Express Edition

·         Open Watcom compiler.


Start by loading the Visual C++ IDE.

From the File menu, click New and select Project. Select Win 32 on the Project Types tree. In the name box, enter example1. You can change the location to wherever you want to store the examples. It defaults to a folder within Documents and Settings. This folder holds the source code and project files. If you want a new directory leave the Create directory for solution checkbox ticked.

Click OK. You will now see the Win 32 Application Wizard. You can press Finish or click Next to see the Application Settings. For example 1 we can keep the default settings.

Now type in a couple of lines so you end up with this. Press Ctrl + s (ie Hold the ctrl key down and press the s key) and this will save the file. You can also click File, Save example1.cpp or click the single floppy disk image just below and between the View and Project menu commands.

 // example1.cpp : main project file.
 #include “stdafx.h”
 #include <iostream>
 using namespace std;
 int main(void)
 cout << “Hello World”;
 return 0;

Press the F7 and this will compile. Before you run it, click the line

 return 0;

and press the F9 Key. You can run it by pressing F5. It will open a console window and output “Hello World”. Press F5 and it will continue running and exit. Without the Break Point, it would just open the Windows, print Hello World and then close too fast to notice.

A Breakdown of Example 1, Line By Line.

The first two lines include headers for stdafx.h and iostream. The first one is needed to build applications. Afx is short for Application Framework and this includes the support code needed to turn a .obj file into a fully executable file. (This is only for Microsoft compilers). When you compile C++ files, they are transformed into individual object code files. Each file holds just the object code from the associated .cpp file. Then a linker program runs, which takes all the object files, adds any system files and outputs a fully functioning executable.

 using namespace std;

This tells the C++ compiler that it should use identifiers from the std library namespace. Without it, the first line in the main() block would need to be prefixed with std::.

 std::cout << “Hello World”;

Without the namespace line, every instance of cout would need a std:: or else will not compile.

 cout << “Hello World”;

The statement cout is the equivalent of printf in C. If you prefer to use printf then add the following line.

 #include <stdio.h>

and change the cout to

 printf (“Hello World”) ;

C++ was designed to be compatible with c which is why printf() still works. You can use either. In a later tutorial, I will show all the formatting possibilities with cout.


The advantages of not having software built with pointers and built in garbage collection have been learnt from observing Java


About Mohammad Khazab

%d bloggers like this: