© Slobodan Dmitrović 2020
S. DmitrovićModern C++ for Absolute Beginnershttps://doi.org/10.1007/978-1-4842-6047-0_4

4. Our First Program

Slobodan Dmitrović1 
(1)
Belgrade, Serbia
 
Let us create a blank text file using the text editor or C++ IDE of our choice and name it source.cpp. First, let us create an empty C++ program that does nothing. The content of the source.cpp file is:
int main(){}
The function main is the main program entry point, the start of our program. When we run our executable, the code inside the main function body gets executed. A function is of type int (and returns a result to the system, but let us not worry about that just yet). The reserved name main is a function name. It is followed by a list of parameters inside the parentheses () followed by a function body marked with braces {}. Braces marking the beginning and the end of a function body can also be on separate lines:
int main()
{
}

This simple program does nothing, it has no parameters listed inside parentheses, and there are no statements inside the function body. It is essential to understand that this is the main program signature.

There is also another main function signature accepting two different parameters used for manipulating the command line arguments. For now, we will only use the first form .

4.1 Comments

Single line comments in C++ start with double slashes // and the compiler ignores them. We use them to comment or document the code or use them as notes:
int main()
{
    // this is a comment
}
We can have multiple single-line comments:
int main()
{
    // this is a comment
    // this is another comment
}
Multi-line comments start with the /* and end with the */. They are also known as C-style comments. Example:
int main()
{
    /* This is a
    multi-line comment */
}

4.2 Hello World Example

Now we are ready to get the first glimpse at our “Hello World” example. The following program is the simplest “Hello World” example. It prints out Hello World. in the console window:
#include <iostream>
int main()
{
    std::cout << "Hello World.";
}

Believe it or not, the detailed analysis and explanation of this example is 15 pages long. We can go into it right now, but we will be no wiser at this point as we first need to know what headers, streams, objects, operators, and string literals are. Do not worry. We will get there.

A brief(ish) explanation

The #include <iostream> statement includes the iostream header into our source file via the #include directive. The iostream header is part of the standard library. We need its inclusion to use the std::cout object, also known as a standard-output stream. The << operator inserts our Hello World string literal into that output stream. String literal is enclosed in double quotes "". The ; marks the end of the statement. Statements are pieces of the C++program that get executed. Statements end with a semicolon ; in C++. The std is the standard-library namespace and :: is the scope resolution operator. Object cout is inside the std namespace, and to access it, we need to prepend the call with the std::. We will get more familiar with all of these later in the book, especially the std:: part.

A brief explanation

In a nutshell, the std::cout << is the natural way of outputting data to the standard output/console window in C++.

We can output multiple string literals by separating them with multiple << operators:
#include <iostream>
int main()
{
    std::cout << "Some string." << " Another string.";
}

To output on a new line, we need to output a new-line character literal. The characters are enclosed in single quotes ' '.

Example:
#include <iostream>
int main()
{
    std::cout << "First line" << ' ' << "Second line.";
}

The represents an escape sequence, a mechanism to output certain special characters such as new-line character ' ', single quote character ''' or a double quote character '"'.

Characters can also be part of the single string literal:
#include <iostream>
int main()
{
    std::cout << "First line Second line.";
}

Do not use using namespace std;

Many examples on the web introduce the entire std namespace into the current scope via the using namespace std; statement only to be able to type cout instead of the std::cout. While this might save us from typing five additional characters, it is wrong for many reasons. We do not want to introduce the entire std namespace into the current scope because we want to avoid name clashes and ambiguity. Good to remember: do not introduce the entire std namespace into a current scope via the using namespace std; statement. So, instead of this wrong approach:
#include <iostream>
using namespace std; // do not use this
int main()
{
    cout << "A bad example.";
}
use the following:
#include <iostream>
int main()
{
    std::cout << "A good example.";
}

For calls to objects and functions that reside inside the std namespace, add the std:: prefix where needed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.111.183