Program
Program
Program
C++ is a language that has evolved much over the years, and these
tutorials explain many features added recently to the language.
Therefore, in order to properly follow the tutorials, a recent
compiler is needed. It shall support (even if only partially) the
features introduced by the 2011 standard.
If for some reason, you need to use some older compiler, you can
access an older version.
What is a compiler?
Computers understand only one language and that language consists of
sets of instructions made of ones and zeros. This computer language
is appropriately called machine language.
00000 10011110
00000 10011110
00001 11110100
00010 10011110
00011 11010100
00100 10111111
00101 00000000
Even if you cannot really understand the code above, you should be
able to appreciate how much easier it will be to program in the C++
language as opposed to machine language.
Console programs
Console programs are programs that use text to communicate with the
user and the environment, such as printing text to the screen or
reading input from a keyboard.
Here you have instructions on how to compile and run console programs
using different free Integrated Development Interfaces (IDEs):
Structure of a program
The best way to learn a programming language is by writing programs.
Typically, the first program beginners write is a program called
"Hello World", which simply prints "Hello World" to your computer
screen. Although it is very simple, it contains all the fundamental
components C++ programs have:
1 // my first program in C++ Hello World!
2
3
4 #include <iostream>
5
6 int main()
7{
std::cout << "Hello World!";
}
The left panel above shows the C++ code for this program. The right
panel shows the result when the program is executed by a computer.
The grey numbers to the left of the panels are line numbers to make
discussing programs and researching errors easier. They are not part
of the program.
You may have noticed that not all the lines of this program perform
actions when the code is executed. There is a line containing a
comment (beginning with //). There is a line with a directive for
the preprocessor (beginning with #). There is a line that defines a
function (in this case, the main function). And, finally, a line
with a statements ending with a semicolon (the insertion into cout),
which was within the block delimited by the braces ( { } ) of
the main function.
all in a single line, and this would have had exactly the same
meaning as the preceding code.
The source code could have also been divided into more code lines
instead:
1 int main ()
2{
3 std::cout <<
4 "Hello World!"; Edit & Run
5 std::cout
6 << "I'm a C++ program";
7}
And the result would again have been exactly the same as in the
previous examples.
Comments
As noted above, comments do not affect the operation of the program;
however, they provide an important tool to document directly within
the source code what the program does and how it operates.
cout is part of the standard library, and all the elements in the
standard C++ library are declared within what is called a namespace:
the namespace std.