Introduction to C++


Introduction to Computers



A computer is a machine that receives instructions and produces a result after performing an appropriate assignment. Since it is a machine, it expects good and precise directives in order to do something. The end result depends on various factors ranging from the particular capabilities of the machine, the instructions it received, and the expected result.

As a machine, the computer cannot figure out what you want. The computer doesn't think and therefore doesn't make mistakes.

Computer programming is the art of writing instructions (programs) that ask the computer to do something and give a result. A computer receives instructions in many different forms, four of which are particularly important.

The first instructions are given by the manufacturers of various hardware parts such as the microprocessor, the motherboard, the floppy and the CD-ROM drives, etc. These parts are usually made by different companies, setting different and various goals that their particular part can perform. The instructions given to the microprocessor, for example, tell it how to perform calculations, at what speed, and under which circumstances. The instructions given to the motherboard tell it to behave like a city where people and cars can move from one part of the town to another, back and forth, for various reasons; this allows information to flow from one part of the city, I mean one section of the computer, to another.

Once the instructions given to the hardware parts are known, software engineers use that information to give the second sets of instructions to the computer. These instructions, known as an operating system, are usually written by one company. These second instructions tell the computer how to coordinate its different components so the result will be a combination of different effects. This time, the computer is instructed about where the pieces of information it receives are coming from, what to do with them, then where to send the result. This time also the operating system designers impose a lot of behaviors to the computer as a machine. Again this time, some computer languages are developed so that programmers can write applications as the third set of instructions. It is like developing languages that people in a city can use to talk to each other. Consider that from now on (once the OS is developed), people get into the habit of doing things according to their particular culture or taste, speaking different languages that their neighbor doesn't understand... Luckily, the computer, I should say the OS, understands all these languages (I can't guaranty that). Some of the operating systems on the market are: Microsoft Windows 3.X, Corel Linux, IBM OS\2, Microsoft Windows 9X, Apple OS 10, Red Hat Linux, Microsoft Windows Millennium, BeOS, Caldera Linux, Microsoft Windows 2000 etc. A particular OS (for example Microsoft Windows 98) depending on a particular processor (for example Intel Pentium) is sometimes referred to as a platform. Some of the computer languages running on Microsoft Windows operating systems are C++, Pascal, Basic, and their variants.



The actual third set of instructions are given to the computer by you, the programmer, using one or more of the languages that the operating system you are planning to use can understand. Your job is going to consist of writing applications. As a programmer, you write statements such as telling the computer, actually the operating system, that "If the user clicks this, do the following, but if he clicks that, do something else. If the user right clicks, display this; if he double-clicks that, do that." To write these instructions, called programs, you first learn to "speak" one of the languages of the OS. Then, you become more creative... Some of the application programs in the market are Microsoft Word, Lotus ScreenCam, Adobe Acrobat, Jasc Paint Shop Pro, etc.

The last instructions are given by whoever uses your program, or your application. For example, if you had programmed Microsoft Word, you would have told the computer that "If a user clicks the New button on the Standard toolbar, I want you to display a new empty document. But if the user clicks File -> New..., I want you to 'call' the New dialog and provide more options to create a new document. If the same user right-clicks on any button on any of the toolbars, I want you to show, from a popup menu, all the toolbars available so she can choose which one she wants. But if she right-clicks on the main document, here is another menu I want you to display."

At this time, you have probably realized that the users of your programs depend on your techniques as a developer to provide an easy to use application (that's what recruiters and employers call experience and creativity). You depend on the computer language that you are actually using (every computer language has its ups and downs). Your computer language depends on the operating system it is running on (different 

operating systems have different strengths and weaknesses). The operating system depends on the microprocessor or the machine it is running in (the biggest difference between two microprocessors is the speeds at which each processes information).

Your interest here is on the computer languages, since you are going to write programs. There are various computer languages, for different reasons, capable of doing different things. Fortunately, the computer can distinguish between different languages and perform accordingly. These instructions are given by the programmer who is using compilers, interpreters, etc, to write programs. Examples of those languages are Basic, C++, Pascal, etc.

Introduction to Header Files

C++ is a huge language so much that it uses various sets of instructions from different parts to do its work. Some of these instructions come in computer files that you simply "put" in your program. These instructions or files are also called libraries. To make your job easier, some of these libraries have already been written for you so that as you include them in your program, you already have a good foundation to continue your construction. Yet, some of these libraries have their limitations, which means you will expand them by writing or including your own libraries.

As noted already, there are libraries previously written for you. One of them asks the computer to receive keyboard strokes from you the user (when you press a key) and another asks the machine (the computer performing some operations) to give back a result. The libraries are files that you place at the beginning of your program as if you were telling the computer to receive its preliminary instructions from another program before expanding on yours. The libraries are (also) called header files and, as computer files, they have the extension ".h". An example would be house.h, or person.h. As you see, they could have any name; when you start creating your own libraries, you will give your files custom and recognizable names.

The first library we will be interested in is called iostream. It asks the computer to display stuff on the monitor's screen.

To see how to put a library in your program, you put it at the beginning of the file. Here is an example:


To use a library in your program, you simply include it by using the word "include" before the name of the library, as follows:

include iostream.h

Since this is a computer language, the computer will follow particular instructions to perform appropriately, which will make this language distinct from the everyday languages. C++ has some words it treats specially and some that will completely depend on you the programmer. For example, the word "include" could be a special word used by C++ or a regular you want to use in your program. In this particular situation, if you want the computer to "know" that the word "include" means, "I want to include the following library", you will have to append a special sign to it. The pound sign "#" will do just that. Therefore, to include a library, you precede the include word with the # sign.

Here is an example:

#include iostream.h

There are usually two kinds of libraries or files you will use in your programs: libraries that came with C++, and those that you write. To include your own library, you would enclose it between double quotes, like this

#include "books.h"

When you include a library that came with C++, you enclose it between < and > as follows:

#include <iostream.h>

Following this same technique, you can add as many libraries as you see fit. Before adding a file, you will need to know what that file is and why you need it. This will mostly depend on your application. For example, you can include a library called stdio like this:

#include <iostream.h>
#include <stdio.h>

Introduction to Namespaces

A namespace is a section of code, delimited and referred to using a specific name. A namespace is created to set apart a portion of code with the goal to reduce, otherwise eliminate, confusion. This is done by giving a common name to that portion of code so that, when referring to it, only entities that are part of that section would be referred to.

Because C++ is so huge, its libraries are created in different namespaces, each with a particular name. To use an existing namespace in your program, you must know its name. To use such a namespace, you can type the using namespace expression followed by the name of the namespace and a semi-colon. For example, to use a namespace called django, you would type:

using namespace django;

One of the namespaces used in C++ is called std. Therefore, to use it, you can type:

using namespace std;

After typing this, any part of the namespace becomes available to you. The iostream library we mentioned above is part of the std namespace. When you use it, you don't need to include the extended of the iostream file. For this reason, you can start your program with:

#include <iostream>
using namespace std;



C++ Projects


C++ Instructions

C++ works by giving (separate) instructions to the computer. These instructions can be treated as assignments. On this site, such an assignment will be called a function. The primary function used in C++ is called main. To distinguish a function from the other types of things you will be using in your programs, a function's name is followed by an opening and a closing parentheses. For example, the main function will always be written at least as main(). When we perform a better study of functions, we will learn more about functions, their parentheses, and other related issues.

When a program is written and you ask the computer to "execute" it, the first thing to look for is the main() function. This means that every C++ program should have the main() function. Because a function is an assignment, in order to perform its job, a function has a body; this is where the behavior (assignment) of the function would be "described". The body of a function starts with an opening curly bracket "{" and closes with a closing curly bracket "}". Everything in between belongs to, or is part of, the function. Therefore, the main() function can be written as:

main() {}

As we learned that we should (must) always include the libraries that we would need, our program now would include main(). Whenever you create a program, it is important to isolate any inclusion of a library on its own line. Here is an example:

#include <iostream>
using namespace std;

C++ is the computer language we are going to study to write programs. C++ is a very universal language, it can be used to write programs for Linux, MS Windows, Macintosh, BeOS, Unix, etc. C++ is very powerful and can be used to create other compilers or languages, it can also be used to write an operating system. This means that you can use C++ to create/write your own computer language. You can also use C++ to create/write your own compiler; this means that, using C++, you can create your own implementation of C++, Pascal, Basic, Perl, or any other existing or non-existing language.

There are many products you can use to create a program in C++. Before a program is made available, it is called a project because you are working on it. Although in the beginning you will usually be working alone, most programs involve a lot of people. That is why during the development of a program or software product, it is called a project. Each one of the available environments provides its own technique(s) of creating a C++ program or working on a C++ project. Therefore, the person who, or the company that, made the environment available to you must tell you how to use that environment (it is neither your responsibility, nor the C++ Standard’s job to tell you how to create a program or how to start a project). I will try to cover those that I know.

The programs we will be creating on this site are called console applications. They can also be called Bash programs (especially on Unix/Linux). The technique you follow to create a project depends on the environment you are using.


Executing a Program

To see what your program does, you need to realize that the lines we have typed are English language instructions asking C++ to perform the main() function. Unfortunately, the computer doesn't understand what all of this means (to a certain extent). The computer has its own language known as the machine language. So, we need to translate it in a language the computer can understand. A program was created to that effect and supplied to you with C++. This is what we call a compiler.

In the past, a program used to be created from various parts all over the computer, some of the techniques are still used to "debug" a program to isolate problems or "bugs". Since this is a small program, we will just ask the computer to "execute" it and see the result. Throughout this site, the words (or verbs) "execute" and "run" will be used interchangeably to mean the same thing.

The C++ language doesn't define how to create a project. When you buy or acquire a c++ compiler, its documentation should tell you how to create and execute a project. We describe here how how to create a project in most familiar environments. If you have an environment or compiler that is not in our list, consult its documentation to know how to use it.

Author Note One of our most valuable goals in writing a site is to avoid including in a program an issue that has not previously been addressed or explained. This site is written as a (general) reference towards the C++ language. To learn C++, you need a C++ compiler, and we describe how to create a C++ project with some of the most commonly used compilers or programming environments. As it happens, and as you may have noticed, different companies (and different individuals for that matter) choose to implement a language as they see fit.

Depending on the programming environment you are using, even depending on how you create your program (for example KDevelop, Borland C++ Builder, and Microsoft Visual C++ all provide more than one way to create or start a console application), sometimes you have a starting empty file or a file with a few lines. Whatever is in the file, you do not need to delete it. For example, KDevelop displays a commented message in the file. You should not delete that text and it will never interfere with your program. Borland C++ Builder opens a file with a couple of "#pragma" lines. You will never have any reason to delete those lines, although you can, without any risk; but since they do not affect your program, why waste your time deleting them?

Depending on the programming environment you are using and how you create your program, the first file may display a line as #include <iostream.h> or another #include line. The file may also have a main() function already included. Here is how we will deal with this issue:

  • If the file displays a line with #include Something, leave it as is. It will not negatively affect your program. Such a file has been tested
  • If the file displays a line with #include <iostream.h>, leave it like that and continue with our other instructions
  • If the file is empty or it does not include a line with #include at all, then you will just follow our instructions and type them as given
  • If the file already includes the main() function, with a line like int main(Something), use that main() function for the exercises in this book. Unless stated otherwise, that function is ready for you and don't modify the Something part between the parentheses.

From now on, you will sometimes be asked to create a project. Follow the instructions of your compiler as we have seen above.


Project Creation


Creating and Executing a Dev-C++ 4 Application

Dev-C++ is a free programming environment. To get it, you can download it from http://www.bloodshed.net. If you decide to use it, you should help the developers with a financial contribution.

  1. Start Dev-C++ 4
  2. On the main menu, click File -> New Project...
  3. On the New Project dialog box, click the Project property sheet if necessary.
    Click Console Application
  4. Click OK.
  5. On the subsequent New Project dialog box, type Exercise to change the name of the project:
  6. Click OK. You will be asked to create a location for the project.
  7. Click the Create New Folder button .
  8. Type Exercise1 and press Enter.
  9. Double-click Exercise1 to display it in the Save In combo box:
  10. Click Save.
  11. Because the project has already been saved, it is better to save your C++ files as you go. As it happens, Dev-C++ has already created the first C++ file for you.
    Change the contents of the file as follows:
    #include <iostream.h>
    #include <stdio.h>
    int main(int argc, char *argv[])
    cout << "C++ is Fun!!!";
    return 0;
  12. To save the current C++ file, on the Main toolbar, click the Save button
  13. Type Exo as the name of the file.
  14. Click Save.
  15. To execute the program, on the main menu, click Execute -> Compile
  16. After the program has been compiled, click Execute.
  17. After viewing the program, press Enter to close the DOS window to return to Dev-C++

Borland C++BuilderX

Borland C++BuilderX is a commercial programming environment developed by Borland. To help programmers, Borland published a free version, called Personal Edition, that you can download and use for your lessons.

  1. On the main menu of C++BuilderX, click File -> New...
  2. In the Object Gallery dialog box, click New Console
  3. Click OK
  4. In the New Console Application - Step 1 of 3, enter the name of the new application in the Name edit box. In this case, you can type Exercise1
  5. Click Next
  6. In the New Console Application Wizard - Step 2 of 3, accept all defaults and click Next
  7. In the New Console Application Wizard - Step 3 of 3, click the check box under Create
  8. Click Untitled1 and delete it to replace it with Exercise
  9. Click Finish
  10. In the Project Content frame, double-click Exercise.cpp to display it in the right frame
  11. To execute the application, on the main menu, click Run -> Run Project

Borland C++ Builder (Console) Applications

Borland C++ Builder is a commercial programming environment developed by Borland. To get, you usually must purchase it.

  1. To create a console application in Borland C++ Builder, from the main menu, click File -> New (or File -> new -> Other):
  2. From the New Items dialog box. click the Console Wizard button and click OK.
  3. From the Console Wizard dialog box, click the C++ radio button and the Console Application check box (the other options are up to you but we will not use them in this tutorial):
  4. Click OK.
    A skeleton program is created for you. For this lesson, I would ask you to delete everything that appears in the Code Editor and type the two lines above, but leave it there. I will not address what all those words mean at this time and we don't even need them and don't care.
  5. Change the program as follows:
    #include <iostream.
    #include <conio>
    using namespace std;
    #pragma hdrstop
    #pragma argsused
    int main(int argc, char* argv[])
        cout << "\nPress any key to continue...";
        return 0;
  6. To execute the program, on the main menu, click Run -> Run
  7. After viewing the result, press Enter to close the DOS window and return to Borland C++ Builder.

Linux C++ (Terminal) Applications

Most, or all, Linux distributions provide various free C++ compilers. The following instructions are from Red Hat Linux 7.2.

  1. To create a C++ application, open the Home Directory (it should have a shortcut on the top left corner of the desktop; otherwise, from the Taskbar, click Start -> KDE menus -> Home Directory)
  2. The title bar and the address combo box should display your username. For this exercise, I am logged with the root username. Therefore, my title bar and the address combo box display file:/root
    With your username selected, right-click in the right frame and click Create New -> Directory...
    New Directory
  3. Type Exercise1 and click OK
  4. On the right frame, make sure the Exercise1 directory is created (because I will refer to it).
  5. Start a text editor. I use gedit, which is available at Start -> Programs -> Applications -> gedit
  6. In the text editor, type the following:
    #include <iostream.h>
    int main()
        cout << “C++ is fun!!!\n”;
        return 0;
  7. Save your file. If you are using gedit like me, on the main menu, click File -> Save As... 
  8. Under the Directory header in the left list, double-click the Up One Level button ../
    If you see your user name in the left list, fine. Otherwise, double-click ../ again.
  9. On the left list, double-click your user name (for me that would be root/) to open you personal directory
  10. On the left list, double-click the Exercise1/ directory we created.
  11. In the Selection: text box, type the name of the file as Exo.cpp and click OK
  12. To execute the program, open the Terminal: Start -> System -> Terminal.
    In the Terminal window, you should see [UserName@localhost Username]$
    For example, mine is [root@localhost root]$
  13. This ensures that you are in your personal directory. To change to the directory that hosts your exercise, type cd Exercise1
    Now the new folder should be inside the square brackets. Mine is [Username@localhost Exercise1]$
  14. To compile the program, we will use the free g++ compiler. Therefore, type:
    g++ Exo.cpp
  15. For example, on mine, I type [jezoo@localhost Exercise1]$ g++ Exo.cpp
  16. You may receive one warning. For now, don't worry.
  17. To execute the program, type ./a.out and press Enter
  18. This is because the executable file, named a with the extension .out has been created one folder up from the actual location of the C++ file.
    For example, on mine, I type [root@localhost Exercise1]$ ./a.out
  19. To check the a.out file that was created when compiling, return to the Home Directory window and navigate to the /home/UserName/Exercise1/Exo

KDevelop C++ Projects

KDevelop is a free programming environment available for the Linux operating system. In some cases, when installing the operating system, you may be prompted whether you want to install KDevelop. Even after the installation, you can add it. If you didn't install it or don't have it on CD or DVD, you can download it free from http://www.kdevelop.org

  1. To create program in KDevelop, start KDevelop by clicking Start -> Development ->KDevelop
  2. On the main menu, of KDevelop, click Project -> New...
  3. From the ApplicationWizard dialog box, in the list or projects, under the Terminal section, click C++ and click Next.
    App Wizard
  4. In the Project Name edit box, type the name of the project. In this case, type Exercise2 and leave the other edit boxes "as is"; in other words, whatever they contain is fine
  5. Uncheck all of the check boxes (make them empty). This is just an option. If you leave them checked, the compiler would generate (a lot of) code for you and I will not have had time to explain those things to you.
  6. Click Next
  7. Make sure the VCS Support is set to NONE and click Next.
  8. Uncheck the headertemplate for .h-files check box (make it empty). Once again, we don't want the compiler to generate code that we haven't learned yet.
  9. Click Next.
  10. Uncheck the headertemplate for .cpp-files check box.
  11. Click Next.
  12. Notice the empty window: KDevelop is ready to create the project.
  13. Click Create.
  14. KDevelop will need a few seconds to create the project. When it has finished, its last line should be READY
  15. Therefore, click Exit.
  16. To create a (source) file, on the main menu, click File -> New...
  17. From the New File dialog box, click C/C++ File (*.cpp,*.c,*.cc,*.C ...)
  18. Type Main for the name of the file. Therefore, the Filename edit box should display Main.cpp (you can name the file anything you like, such as Exo or Exercise).
  19. Make sure the Add to Project check box is checked and click OK.
  20. Leave the grayed section on top of the file (it is just a set of comments that you don't need to delete; they will not affect your program).
  21. In the empty section of the file, type:
    #include <iostream>
    using namespace std;
    int main()
        cout << “C++ is fun!!!\n”;
        return 0;
  22. To execute the program, on the main menu, click Build -> Execute
    23. After viewing the program, press Enter to close the bash window

Microsoft Visual C++ (5, 6) Console Applications

Visual C++ is a commercial programming environment developed by Microsoft. You must purchase it if you want to use it (normally, because there is a new version of Visual C++, you may not find Visual C++ 6 anymore from Microsoft).

  1. Start Microsoft Visual C++.
  2. On the main menu of Microsoft Visual C++ or Microsoft Visual Studio, click File -> New...
  3. Click the Projects property sheet.
  4. Click Win32 Console Application.
  5. In the Location box, type a drive followed by a folder name. For example, type C:\Programs\MSVC
  6. In the Project Name, type Exercise1
  7. Click OK.
  8. In the Win32 Console Application Step 1 of 1, click the An Empty Project radio button
  9. Click Finish.
  10. You will be presented with another dialog box. Click OK.
  11. To create a C++ file, on the main menu, click File -> New...
  12. In the New dialog box, make sure the Files property sheet is selected.
  13. Click C++ Source File
  14. In the File Name, type a name such as Exercise and click OK
  15. From what we have learned so far, change the contents of the file as follows:
    #include <iostream>
    using namespace std;
    int main()
        cout << "C++ is fun!!!\n";
        return 0;
  16. To execute the program, on the main menu, click Build -> Execute Exercise1.exe
  17. When asked to save the project, click Yes
  18. After viewing the result, press Enter to close the DOS window and return to MSVC.

Microsoft Visual C++ .NET Console Applications

Visual C++ .NET is a commercial programming environment developed by Microsoft. It is usually shipped with Microsoft Visual Studio .NET but in some cases, you can purchase it alone. In fact, there is a version made for students and sold at a low price.

  1. Start Microsoft Visual Studio .NET.
  2. On the main menu of Microsoft Development Environment, click File -> New -> Project...
  3. On the New Project dialog box, in the Location box (bottom of the dialog box), type a drive followed by a folder name such as C:\Programs\MSVC .Net 
  4. In the Project Type, click Visual C++ Projects
  5. In the Templates list view, click Win32 Project
  6. In the Name box, type Exercise1
  7. Click OK.
  8. In the Win32 Application Wizard - Exercise1 dialog box, click Application Settings
  9. In the Application Type section, click the Console Application radio button
  10. In the Additional Options section, click the Empty Project check box
  11. Click Finish.
  12. To create a new C++ file, on the main menu, click Project -> Add New Item... Or, on the Solution Explorer, right-click Exercise1 -> Add and click Add New Item...
  13. In the Categories list, make sure Visual C++ or C++ is selected.
  14. In the Templates list view, click C++ File (.cpp)
  15. In the Name box, replace the contents with Exercise
  16. Click Open
  17. From what we know about C++ already, change the contents of the file with:
    #include <iostream>
    using namespace std;
    int main()
        cout << "C++ is fun!!!\n";
        return 0;
  18. To execute the program, on the main menu, click Build -> Execute Exercise1.exe
  19. When asked to save the project, click Yes
  20. After viewing the result, press Enter to close the DOS window and return to MSVC.

Code Fundamentals


Using cout

There are various ways data get in your program. The first means of entering data in a C++ program is by typing it from the keyboard. Another way would be to instruct the program to receive data from another program then process it. A program can also receive its data or part of it from other hardware devices such as a CD-ROM, a DVD-ROM, a modem, a video camera, etc.

To display stuff on the monitor, C++ uses operators. The operator used to display something on the screen is called cout (pronounce see - out) (actually, cout is a class and not an operator, but we haven't learned what a class is). The cout word is followed by the extraction operator <<, then some simple rules to display anything. For example, to display a word or sentence, you include it in double quotes " and ".

While you are giving these instructions, you type them in your program. Each C++ instruction is terminated by a semi-colon ";".

We have already seen that a program is a set of instructions you give to the computer. These instructions are given inside of functions. This means that an instruction is part of a function. The thing we want to display in our program will be performed by the main() function. In other words, the instruction to display something will be given in main().

Here is an example: 

#include <iostream>
using namespace std;

int main()
cout << "Computer Programming With C++";
return 0;


The most basic documentation you will (have to) perform is to put comments as much as you can. Comments help you and other people who read your code to figure out what you were doing.

Comments are not read by the compiler while executing your program. That means you write them in everyday conversation. There are two usual ways of including comments in your program. To comment the contents of one line, you start it with double forward slashes like this //

Here is an example:

// include the first library
#include <iostream>
using namespace std;

int main()
// Here is a simple sentence
cout << "Hi, this is my program!";
return 0;
// The end of my program

You can include many lines of comments in your program. To do that, comment each line with the double slashes. An alternative is to start the beginning of the commented paragraph with /* and end the commented section with */

Here is another commented version of our program: 

// The exo.cpp program
// Include the ostream library
#include <iostream>
using namespace std;

int main()
/* Here is a simple sentence that I want to display
when the program starts. It doesn't do much.
I am planning to do more stuff in the future. */

cout << "Hi, this is my program!";
return 0;
// The end of my program

Home Copyright © 2008 Yevol Next