User Tools

Site Tools


My first C++ program

Here, we will talk about the basics of the C++ programming language. Because we do not want to repeat ourselves, we will not talk again for the library, function and variable context, because we have already discussed them in the C programming language section. We will only describe the specific C++ characteristic


The first program

Yes, we will contnue with our “Hello World” tradition.

#include <iostream>
using namespace std;
int main()
  cout << "Hello World.\n";
  return 0;

The lines numbered with 1, 2 and 6 are specific for C++ and differ from the C language. Therefore, we will not discuss the other lines.

First, notice the “include <iostream>” line, and compare it with the appropriate C line. You will notice that in the C++ language, the header files do not have the .h extension. According to the new ANSI-C++ standard, the header files should not longer have the .h extension, as C and early C++ compilers used to work with. And not only that. The new standard involves adding the c letter to the standard C header files. So, a typical C header named stdio.h , now will be cstdio. And one more thing. All functions and classes are under the std namespace, which will be discussed later.


Just notice the second line. Namespaces will be discussed in the Advanced Concept.

Input and Output

First, to get the input and output abilities, you need the appropriate header file. And that is iostream for console inoput/output and fstream for file input/output.

These console input/output streams exist:

  • cout (for output)
  • cin (for input)

Cout stands for Console Output. You can use it by typing the command “cout” and using the output operator ”«” :

cout <<"Test.\n";

The output operator is used to direct the output to the standard output, usually your screen. We can print variables too:

cout «“Value of variable test: ” «test «“\n”;

Notice that in cout, there are no format indentifiers, like in the C language. That makes the job of the programmer much easier.

Cin stands for Console Input. It uses the input operator, the opposite one of the output operator. It is defined as ”»”:

int number;

cout <<"Enter number: ";
cin >> number;

Cin , like cout, does not use format identifiers. It just uses the input operator to insert the value into the variable specified. As you can see, the usage is very simple.

handbook/handbook/programing_1.txt · Last modified: 2010/04/15 21:18 (external edit)