Guide | How To How to start programming in C++ (Part 1 of 5)

The associated guide may contain user-generated or external content.

JM Safe

Level 39
Thread author
Verified
Top Poster
Apr 12, 2015
2,882
Hi everyone! :)

So... Yes, I decided to start this mini and really simple tutorial about this very important language: C++.
I haven't an enormous experience with C++, but I want to explain what I know... so let's start!

The four most important points of a OO language (Object Oriented language)
„
  • Encapsulation
  • Data Hiding
  • Inheritance
  • Polymorphism
Encapsulation:

We will use an object absolutely well defined,of which we know the characteristics and the results of its implementation, but completely created by someone else.

Data Hiding:

The Data Hiding is a development technique used in object-oriented programming language (OOP) to hide the details of the internal objects. Data hiding ensures data access to the members of the class and protects the objects by unwanted changes.

Inheritance:

The new class will then be adapted to new needs problems even though it won't have required a lot of work as to create it from scratch but simply be an extension something which will inherit the legacy used features until then spreading them apart.

Polymorphism:

Polymorphism means that property that one same object with the same name can be associated with multiple shapes and then different consequences in the same use of its different manifestations.


So, let's start with some code!

The first example: Hello, world!


Code:
#include <iostream.h>int main()[/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR]
[COLOR=#00b300][COLOR=#000000][COLOR=#00b300][COLOR=#000000][COLOR=#00b300][COLOR=#000000]{

Cout<< “Hello, MalwareTips!” <<endl;
return 0;
}

If you use Linux operating system, there will be the text editor emacs and compiler g ++. The corresponding commands are:

$ emacs example.cpp &

invokes the emacs editor that opens (or create if not present) the file example.cpp

$ g ++-o example example.cpp

invoking the compiler g ++ to compile the program written in the file example.cpp, the result is written into the executable file example.

$ example

runs the executable example

The above program is the first program that most beginners writes and the result is the appearance of the inscription "Hello MalwareTips" on the screen. It is one of the simplest programs you can write in C++ but it already contains the main component of any C++ program. See them one at a time:

//My first program in C++

This is a comment line. All lines that start with two bars (//) are treated as comments and have no effect on the behavior of the program. They can be used by the programmer to include in the program code some brief explanations and observations. In our case the row contains a brief explanation of what the program should do.

#include <iostream.h>

The sentences that start with the number sign (#) are the compiler's preprocessor directives. They are not executable statements but only signs for the compiler. In our case the sentence #include <iostream.h> tells the compiler preprocessor to include the iostream standard library h. This particular file contains the declarations of the basic input-output operations defined in the C++ standard library and is included because these transactions will be used later in the program.

int main ()

This line is the beginning of the Declaration of the main function. The main function is the starting point to begin executing any C++ program. It is irrelevant where your program in which it appears that function, it is always the first to be executed. Obviously it is essential that each program contains a main function.
The main is followed by a pair of parentheses () because it is a function. In C++ all functions are followed by a pair of parentheses () that may contain arguments. The immediate aftermath (the header) of the function is the content (body) of the function within braces ({}).
"Hello MalwareTips" cout < <;
This instruction makes the most important subject in the standard output stream cout is the program of C++ (usually addressed to the screen), and the effect of education is to insert a sequence of characters (in our case "Hello MalwareTips") on this output stream. The Declaration of cout is located in iostream.h, which is why we had to include it. We note that the sentence ends with a semicolon (
Code:
;
). The semicolon indicates the end of the statement and must be placed at the end of each statement (one of the most common errors is forgetting the semicolon at the end of a statement).

return 0;

The return statement terminates the function main () and returns what is outlined below, in our case 0. This is the normal way to end a program whose execution took place without error. As we shall see in the next few examples, all C++ programs end with a phrase like this. (Many compilers put a return statement at the end of a function.)

Therefore, not all the rows in a program denote an action. There are lines that contain only comments (lines that start with//), rows that contain instructions for the precompiler (lines that start with #), lines that begin the Declaration of a function (in our case, the main function) and finally rows that contain executable statements ("Hello MalwareTips" as cout < <);, the latter are contained in the body of the function (the block delimited by braces ({}) of the main function.

The program was divided into multiple lines to make it more readable, but this is not necessary. For example, instead of

Code:
int main (){[/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR]
[COLOR=#00b300][COLOR=#000000][COLOR=#00b300][COLOR=#000000][COLOR=#00b300][COLOR=#000000]"Hello MalwareTips" cout < <;
return 0;
}

We could write:

Code:
int main () {cout "Hello MalwareTips"; return 0 < <;}

all in the same row and this would have exactly the same meaning.

In C++ the separation between directions is indicated by the semicolon (
Code:
;
) ending each one. Splitting the program into multiple lines is intended to make the program more readable to people who read it, for the compiler that doesn't make any difference.


Here's a program with some other statement:


//My second program in C++

Code:
#include <iostream.h>[/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR]
[COLOR=#00b300][COLOR=#000000][COLOR=#00b300][COLOR=#000000][COLOR=#00b300][COLOR=#000000]
int main ()
{
"Hello MalwareTips" cout < <;
cout < < "The first program:)";
return 0;
}

In this case we used the operation twice in two separate statements cout < <. Once again, the separation into multiple lines of the program solely to make it readable, in fact one could write:

Code:
int main () {cout cout MalwareTips "Hello!"; < < < < "The first program:)"; return 0; }

We could also split the text of the program to a greater number of rows if we deem convenient:

Code:
int main (){[/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR]
[COLOR=#00b300][COLOR=#000000][COLOR=#00b300][COLOR=#000000][COLOR=#00b300][COLOR=#000000]cout < <
"Hello MalwareTips";
cout
"The first program:)" < <;
return 0;
}

and the result would be exactly the same.

This rule does not apply to the preprocessor directives (those starting with #) because they are not real instructions. Have any lines will be read and interpreted by the preprocessor and which will disappear from the code that will be compiled by the compiler. They must be each on its own line and do not require the semicolon (
Code:
;
) to finish.


Comments

Comments are pieces of text that are ignored by the compiler. They do nothing. Their purpose is to allow the programmer to add notes or short descriptions in the midst of program instructions.

In C++, there are two ways to insert comments:

//comment line
/* comment block */

The first considers comment everything after the pair of bars (//) to the end of the line. The second, called the comment block, consider comment anything that is between the pair of characters/* and before next occurrence of character pair */possibly even multiple lines of text.

Thank you all for reading the part 1! ;)
 
Last edited:

Barouch

New Member
Jul 25, 2018
3
Hello,
thanks for this little reminder, but starting with OOP programming is such an advanced topic for newbies, i thought that you will start with basics(variables, conditions, arrays) anyway good luck.
 
  • Like
Reactions: AtlBo

About us

  • MalwareTips is a community-driven platform providing the latest information and resources on malware and cyber threats. Our team of experienced professionals and passionate volunteers work to keep the internet safe and secure. We provide accurate, up-to-date information and strive to build a strong and supportive community dedicated to cybersecurity.

User Menu

Follow us

Follow us on Facebook or Twitter to know first about the latest cybersecurity incidents and malware threats.

Top