C++ is going to be the theme of this course, it is a computer programming language. A computer programming language is a set of symbols, rules, and meanings that constitute an artificial language that humans can use for specifying the instructions of a computer program.
A C++ program is a collection of C++ source files. A C++ source file is a text file containing C++ source code.
Since C++ programs are stored as text files in the file system, you will need a text editor to create and edit C++ programs. On many Linux distributions that have a graphical desktop environment, you should be able to find a text editor in the application menu. If you are using Linux and you see a button labeled "Activities" in the top-left corner, click on it and type "gedit" into the search bar and press Enter; you have now opened a text editor.
The C++ programming language is known as a compiled language because it requires the use of a special program known as a compiler so that a C++ program can be translated into a program image that the computer can execute.
A popular C++ compiler comes from the GNU Compiler Collection, it is referred to as g++ from the command shell. To compile your C++ program into a program image, you must execute the g++ command with the paths to your C++ source files as arguments.
The following is C++ source code for an entire C++ program:
#include "std.h"
int main()
{
cout << "Hello world!\n";
return 0;
}
The file "std.h" will be required for this program to be complete: you can download it here.
Suppose this C++ source code was saved to a file named "hello.cpp", residing in the current working directory together with the "std.h" file. Then, to compile "hello.cpp" the command would be:
g++ hello.cpp
If you receive an error that says "No such file or directory", then your current working directory does not refer to the same directory of where you saved your C++ source files. Use the cd command to change to the proper directory. If you saved the "hello.cpp" and "std.h" files in your Documents directory, then the command cd ~/Documents should be used before attempting to compile (The ~ symbol is a special character within bash that refers to your "home directory", the directory that contains all of your user files).
After successfully compiling, the compiler will have then generated a file named "a.out", this is the default file name that g++ uses for the program image. The program image can then be executed from the command shell with the following command:
./a.out
If all is successful, you should see the line "Hello world!" printed to the terminal.
This type of program is known as a console application because it must be used through a terminal emulator or some other text-only display.
At the top-most level, all C++ source code is composed of a sequence of declarations. Each declaration introduces the name of an entity within the C++ program. Let's take a look at the "Hello world!" program again but with line numbers visible on the side:
1 #include "std.h"
2
3 int main()
4 {
5 cout << "Hello world!\n";
6 return 0;
7 }
Everything from line 3 to line 7 is a single declaration. The name of what is being declared is "main" and the kind of entity being declared here is a function.
All C++ programs must have exactly one function named "main", the main function denotes the entry point of the C++ program. The first C++ statement to be executed in a C++ program, after initialization takes place, is the first statement that resides in the function body of the main function.
Lines 4-7 make up the function body of main. Lines 5 and 6 are the statements inside this function body.
Line 5 is an expression statement. An expression statement is an expression terminated by a semicolon. An expression is a sequence of operators and their operands; expressions specify a form of computation. Line 5 contains the operator <<, it is a binary operator because it requires 2 operands; in this case, it's left operand is cout and it's right operand is "Hello world!\n". What this expression signifies is that the characters "Hello world!\n" are being inserted into the object named cout, this normally results in the side effect of the inserted data being displayed in the terminal window. The '\n' character inside of the right operand is an escaped character, it represents the "line feed" or "new line" character.
Line 6 is a return statement, terminating the execution of the current function. Any return statement executed inside of the main function terminates the entire program. This return statement provides a return value of 0, signifying a successful termination, any non-zero return value signifies an unsuccessful termination.
Line 1 is not C++ source code, it is an include directive that is meant for the preprocessor. The preprocessor is a program that processes out all lines that begin with '#', which are known as preprocessor directives, before compilation of C++ source code begins. The include directive directs the preprocessor to include the contents of another file into the current file on the next line.