Quick learning: C++ Programming in ONE Hour

(By Dr. Leo Hu)


You can create a C++ program in only one hour, even though you have no knowledge of programming.

Let's begin!

1.Define a C++ program

A C++ program consists of a function called main() that looks like

int main() {

    return 0;

}

where 

return 0;

 and means that function main() has successfully completed. The word return is a reserved word (or called keyword) in programming language C++.  

Attention: A reserved word, or a keyword, cannot be used to name any of the programmer's identifiers

Note: The complete set of reserved keywords can be reviewed at https://en.cppreference.com/w/cpp/keyword

Attention: a statement in C++ shall end with a semicolon ';'. 

The keyword return is followed with an integer that has been defined by the program caller. Here the program caller is the operating system. Microsoft Windows operating systems has defined

0 - success

1 - Informational

2 - Warning

3 - Error

Therefore, statement 

return 0;

means the program has completed with success. Other operating systems have the same effect. 

Tip: To increase program source code readability, we may add spaces between items and indentations before statements. We may use spacebar to add space, and tab key to add indentations in a C++ program source code. It doesn't matter how many spaces or how many indentations are added in a C++ program source code, but we should keep in mind that we apply spaces and indentations to beautify our code and improve readability of our programs. In this simple program, we have added a space between int and main(), and an indentation before return statement.  Theoretically, you can write your code in one line, like

int main(){return 0;}

but this is not good programming practice. Dividing a program code in lines is also a way to improve readability. 

2.Add comments

If you want to explain something to your program reader (or remind yourself), you may add comments in your source code. 

Attention: A comment is not to be used by C++ language compiler but by your reader and yourself for understanding the code. The C++ language compiler, which translate a C++ program source code into machine code, simply omits comments in a program source code. 

For example, we may add comments a in the above simple program, and it looks like

// Online C++ compiler to run C++ program online

int main() {

// Write C++ code here

       return 0;

}

In this program, there are two comments that start with double slashes "//". However, a comment can be enclosed between (/*) and (*/), and when a comment is written on more than one line, we must use (/*) and (*/) to enclose it.  For example, the above program can be written as below without any syntax change of C++ program:

  /* Online C++ compiler

to run C++ program online

*/

int main() {

/* Write C++ code here*/

   return 0;

}

3.Add a statement to do something

Let's add a statement to display "Hello World!" on the screen. The program may look like

// Online C++ compiler to run C++ program online

int main() {

// Write C++ code here

std::cout << "Hello world!";

  return 0;

}

Here, statement 

std::cout << "Hello world";

display text Hello world on the screen, where std::cout is an object of the std::ostream class in C++ that represents the standard output stream. It is used to output data to the console or other output devices. By default, it stands the console. Output stream operator (<<) is used to insert data into the output stream. 

Note: terms of object and class will be explained in more depth soon. 

Before we can use any code pre-defined in another file, such as std::cout pre-defined in <iostream>, we must add that file so that the pre-defined code can be used in our program. So, the program should be rewritten as 

// Online C++ compiler to run C++ program online

#include <iostream>

int main()

{

// Write C++ code here

std::cout << "Hello world!";

return 0;

}

Here, #include <iostream>, is called a preprocessor directive in C++ that tells the compiler to include a file, called library, in the source code program.

Hands-on:

Click the link, https://www.programiz.com/cpp-programming/online-compiler/, to use the online C++ programming tool to watch the execution of the program we discussed above.  This tool has an editor in the left pane that host the program source code, and the program execution result shall be displayed in the Output in the right pane as shown in Figure 1 below.