Forums
New posts
Search forums
News
Security News
Technology News
Giveaways
Giveaways, Promotions and Contests
Discounts & Deals
Reviews
Users Reviews
Video Reviews
Support
Windows Malware Removal Help & Support
Mac Malware Removal Help & Support
Mobile Malware Removal Help & Support
Blog
Log in
Register
What's new
Search
Search titles only
By:
Search titles only
By:
Reply to thread
Menu
Install the app
Install
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Forums
Guides
Programming Guides & Questions
How to start programming in C++ (Part 1 of 5)
Message
<blockquote data-quote="JM Safe" data-source="post: 470292" data-attributes="member: 35684"><p><em><strong>Hi everyone! <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite109" alt=":)" title="Smile :)" loading="lazy" data-shortname=":)" /></strong></em></p><p></p><p>So... Yes, I decided to start this mini and really simple tutorial about this very important language: C++.</p><p>I haven't an enormous experience with C++, but I want to explain what I know... <strong>so let's start!</strong></p><p></p><p>The four most important points of a OO language (Object Oriented language)</p><p></p><ul> <li data-xf-list-type="ul"><strong>Encapsulation</strong></li> </ul> <ul> <li data-xf-list-type="ul"><strong>Data Hiding</strong></li> </ul> <ul> <li data-xf-list-type="ul"><strong>Inheritance</strong></li> </ul> <ul> <li data-xf-list-type="ul"><strong>Polymorphism</strong></li> </ul><p><strong>Encapsulation:</strong></p><p></p><p>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.</p><p></p><p><strong>Data Hiding:</strong></p><p><strong></strong></p><p>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.</p><p></p><p><strong>Inheritance:</strong></p><p></p><p>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.</p><p></p><p><strong>Polymorphism:</strong></p><p></p><p>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.</p><p></p><p><span style="color: #00b300"></span></p><p><span style="color: #00b300"><strong>So, let's start with some code!</strong></span></p><p><span style="color: #00b300"></span></p><p><span style="color: #00b300"><span style="color: #000000">The first example: Hello, world!</span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000">[CODE]#include <iostream.h>int main()[/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR]</span></span></p><p><span style="color: #00b300"><span style="color: #000000">[COLOR=#00b300][COLOR=#000000][COLOR=#00b300][COLOR=#000000][COLOR=#00b300][COLOR=#000000]{</span></span></p><p><span style="color: #00b300"><span style="color: #000000"></span></span></p><p><span style="color: #00b300"><span style="color: #000000">Cout<< “Hello, MalwareTips!” <<endl;</span></span></p><p><span style="color: #00b300"><span style="color: #000000">return 0;</span></span></p><p><span style="color: #00b300"><span style="color: #000000">}[/CODE]</span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000">If you use Linux operating system, there will be the text editor emacs and compiler g ++. The corresponding commands are:</span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000"><strong>$ emacs example.cpp &</strong></span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000">invokes the emacs editor that opens (or create if not present) the file example.cpp</span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000"><strong>$ g ++-o example example.cpp</strong></span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000">invoking the compiler g ++ to compile the program written in the file example.cpp, the result is written into the executable file example.</span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000"><strong>$ example</strong></span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000">runs the executable example</span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000">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:</span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000"><strong>//My first program in C++</strong></span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000">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.</span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000"><strong>#include <iostream.h></strong></span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000">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 <strong>#include <iostream.h></strong> 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.</span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000"><strong>int main ()</strong></span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000">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.</span></span></p><p><span style="color: #00b300"><span style="color: #000000">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 ({}).</span></span></p><p><span style="color: #00b300"><span style="color: #000000">"Hello MalwareTips" cout < <;</span></span></p><p style="margin-left: 20px"><span style="color: #00b300"><span style="color: #000000">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 <strong>iostream.h</strong>, which is why we had to include it. We note that the sentence ends with a semicolon ([CODE];[/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).</span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000"><strong>return 0;</strong></span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000">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.)</span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000">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.</span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000">The program was divided into multiple lines to make it more readable, but this is not necessary. For example, instead of</span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000">[CODE]int main (){[/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR]</span></span></p><p><span style="color: #00b300"><span style="color: #000000">[COLOR=#00b300][COLOR=#000000][COLOR=#00b300][COLOR=#000000][COLOR=#00b300][COLOR=#000000]"Hello MalwareTips" cout < <;</span></span></p><p><span style="color: #00b300"><span style="color: #000000">return 0;</span></span></p><p><span style="color: #00b300"><span style="color: #000000">}[/CODE]</span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000">We could write:</span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000">[CODE]int main () {cout "Hello MalwareTips"; return 0 < <;}[/CODE]</span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000">all in the same row and this would have exactly the same meaning.</span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000">In C++ the separation between directions is indicated by the semicolon ([CODE];[/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.</span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000">Here's a program with some other statement:</span></span></p><p></p><p></p><p><span style="color: #00b300"><span style="color: #000000">//My second program in C++</span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000">[CODE]#include <iostream.h>[/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR]</span></span></p><p><span style="color: #00b300"><span style="color: #000000">[COLOR=#00b300][COLOR=#000000][COLOR=#00b300][COLOR=#000000][COLOR=#00b300][COLOR=#000000]</span></span></p><p><span style="color: #00b300"><span style="color: #000000">int main ()</span></span></p><p><span style="color: #00b300"><span style="color: #000000">{</span></span></p><p><span style="color: #00b300"><span style="color: #000000">"Hello MalwareTips" cout < <;</span></span></p><p><span style="color: #00b300"><span style="color: #000000">cout < < "The first program:)";</span></span></p><p><span style="color: #00b300"><span style="color: #000000">return 0;</span></span></p><p><span style="color: #00b300"><span style="color: #000000">}[/CODE]</span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000">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:</span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000">[CODE]int main () {cout cout MalwareTips "Hello!"; < < < < "The first program:)"; return 0; }[/CODE]</span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000">We could also split the text of the program to a greater number of rows if we deem convenient:</span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000">[CODE]int main (){[/COLOR][/COLOR][/COLOR][/COLOR][/COLOR][/COLOR]</span></span></p><p><span style="color: #00b300"><span style="color: #000000">[COLOR=#00b300][COLOR=#000000][COLOR=#00b300][COLOR=#000000][COLOR=#00b300][COLOR=#000000]cout < <</span></span></p><p><span style="color: #00b300"><span style="color: #000000">"Hello MalwareTips";</span></span></p><p><span style="color: #00b300"><span style="color: #000000">cout</span></span></p><p><span style="color: #00b300"><span style="color: #000000">"The first program:)" < <;</span></span></p><p><span style="color: #00b300"><span style="color: #000000">return 0;</span></span></p><p><span style="color: #00b300"><span style="color: #000000">}[/CODE]</span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000">and the result would be exactly the same.</span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000">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];[/CODE]) to finish.</span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000"><strong>Comments</strong></span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000">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.</span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000">In C++, there are two ways to insert comments:</span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000"><strong>//comment line</strong></span></span></p><p><span style="color: #00b300"><span style="color: #000000"><strong>/* comment block */</strong></span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000">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.</span></span></p><p></p><p><span style="color: #00b300"><span style="color: #000000"><strong>Thank you all for reading the part 1! <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite110" alt=";)" title="Wink ;)" loading="lazy" data-shortname=";)" /></strong></span></span></p></blockquote><p></p>
[QUOTE="JM Safe, post: 470292, member: 35684"] [I][B]Hi everyone! :)[/B][/I] 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... [B]so let's start![/B] The four most important points of a OO language (Object Oriented language) [LIST] [*][B]Encapsulation[/B] [/LIST] [LIST] [*][B]Data Hiding[/B] [/LIST] [LIST] [*][B]Inheritance[/B] [/LIST] [LIST] [*][B]Polymorphism[/B] [/LIST] [B]Encapsulation:[/B] 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. [B]Data Hiding: [/B] 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. [B]Inheritance:[/B] 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. [B]Polymorphism:[/B] 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. [COLOR=#00b300] [B]So, let's start with some code![/B] [COLOR=#000000]The first example: Hello, world![/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000][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; }[/CODE][/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000]If you use Linux operating system, there will be the text editor emacs and compiler g ++. The corresponding commands are:[/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000][B]$ emacs example.cpp &[/B][/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000]invokes the emacs editor that opens (or create if not present) the file example.cpp[/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000][B]$ g ++-o example example.cpp[/B][/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000]invoking the compiler g ++ to compile the program written in the file example.cpp, the result is written into the executable file example.[/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000][B]$ example[/B][/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000]runs the executable example[/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000]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:[/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000][B]//My first program in C++[/B][/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000]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.[/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000][B]#include <iostream.h>[/B][/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000]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 [B]#include <iostream.h>[/B] 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.[/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000][B]int main ()[/B][/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000]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.[/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000]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 ({}).[/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000]"Hello MalwareTips" cout < <;[/COLOR][/COLOR] [INDENT][COLOR=#00b300][COLOR=#000000]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 [B]iostream.h[/B], which is why we had to include it. We note that the sentence ends with a semicolon ([CODE];[/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).[/COLOR][/COLOR][/INDENT] [COLOR=#00b300][COLOR=#000000][B]return 0;[/B][/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000]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.)[/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000]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.[/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000]The program was divided into multiple lines to make it more readable, but this is not necessary. For example, instead of[/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000][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; }[/CODE][/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000]We could write:[/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000][CODE]int main () {cout "Hello MalwareTips"; return 0 < <;}[/CODE][/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000]all in the same row and this would have exactly the same meaning.[/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000]In C++ the separation between directions is indicated by the semicolon ([CODE];[/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.[/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000]Here's a program with some other statement:[/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000]//My second program in C++[/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000][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; }[/CODE][/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000]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:[/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000][CODE]int main () {cout cout MalwareTips "Hello!"; < < < < "The first program:)"; return 0; }[/CODE][/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000]We could also split the text of the program to a greater number of rows if we deem convenient:[/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000][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; }[/CODE][/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000]and the result would be exactly the same.[/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000]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];[/CODE]) to finish.[/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000][B]Comments[/B][/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000]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.[/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000]In C++, there are two ways to insert comments:[/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000][B]//comment line[/B][/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000][B]/* comment block */[/B][/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000]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.[/COLOR][/COLOR] [COLOR=#00b300][COLOR=#000000][B]Thank you all for reading the part 1! ;)[/B][/COLOR][/COLOR] [/QUOTE]
Insert quotes…
Verification
Post reply
Top