Results 1 to 11 of 11
  1. #1
    headsup's Avatar
    Join Date
    Apr 2009
    Gender
    male
    Location
    Pa
    Posts
    1,232
    Reputation
    8
    Thanks
    208
    My Mood
    Cynical

    New o C++? Simple Hello world (Added a little extra)

    To whom is this tutorial directed?




    This tutorial is for those people who want to learn programming in C++ and do not necessarily have any previous knowledge of other programming languages. Of course any knowledge of other programming languages or any general computer skill can be useful to better understand this tutorial, although it is not essential.

    It is also suitable for those who need a little update on the new features the language has acquired from the latest standards.

    If you are familiar with the C language, you can take the first three parts of this tutorial as a review of concepts, since they mainly explain the C part of C++. There are slight differences in the C++ syntax for some C features, so I recommend you its reading anyway.

    The 4th part describes object-oriented programming.

    The 5th part mostly describes the new features introduced by ANSI-C++ standard.



    Probably the best way to start learning a programming language is by writing a program. Therefore, here is our first program:


    Code:
    1 // my first program in C++
    2 
    3 #include <iostream>
    4 using namespace std;
    5
    6 int main ()
    7 {
    8     cout << "Hello World!";
    9     return 0;
    10 }


    // my first program in C++

    This is a comment line. All lines beginning with two slash signs (//) are considered comments and do not have any effect on the behavior of the program. The programmer can use them to include short explanations or observations within the source code itself. In this case, the line is a brief description of what our program is.


    #include <iostream>

    Lines beginning with a hash sign (#) are directives for the preprocessor. They are not regular code lines with expressions but indications for the compiler's preprocessor. In this case the directive #include <iostream> tells the preprocessor to include the iostream standard file. This specific file (iostream) includes the declarations of the basic standard input-output library in C++, and it is included because its functionality is going to be used later in the program.


    using namespace std;

    All the elements of the standard C++ library are declared within what is called a namespace, the namespace with the name std. So in order to access its functionality we declare with this expression that we will be using these entities. This line is very frequent in C++ programs that use the standard library, and in fact it will be included in most of the source codes included in these tutorials.

    int main ()

    This line corresponds to the beginning of the definition of the main function. The main function is the point by where all C++ programs start their execution, independently of its location within the source code. It does not matter whether there are other functions with other names defined before or after it - the instructions contained within this function's definition will always be the first ones to be executed in any C++ program. For that same reason, it is essential that all C++ programs have a main function.

    The word main is followed in the code by a pair of parentheses (()). That is because it is a function declaration: In C++, what differentiates a function declaration from other types of expressions are these parentheses that follow its name. Optionally, these parentheses may enclose a list of parameters within them.

    Right after these parentheses we can find the body of the main function enclosed in braces ({}). What is contained within these braces is what the function does when it is executed.



    cout << "Hello World!";

    This line is a C++ statement. A statement is a simple or compound expression that can actually produce some effect. In fact, this statement performs the only action that generates a visible effect in our first program.

    cout is the name of the standard output stream in C++, and the meaning of the entire statement is to insert a sequence of characters (in this case the Hello World sequence of characters) into the standard output stream (cout, which usually corresponds to the screen).

    cout is declared in the iostream standard file within the std namespace, so that's why we needed to include that specific file and to declare that we were going to use this specific namespace earlier in our code.

    Notice that the statement ends with a semicolon character ( ; ). This character is used to mark the end of the statement and in fact it must be included at the end of all expression statements in all C++ programs (one of the most common syntax errors is indeed to forget to include some semicolon after a statement).



    return 0;

    The return statement causes the main function to finish. return may be followed by a return code (in our example is followed by the return code with a value of zero). A return code of 0 for the main function is generally interpreted as the program worked as expected without any errors during its execution. This is the most usual way to end a C++ console program.


    You may have noticed that not all the lines of this program perform actions when the code is executed. There were lines containing only comments (those beginning by //). There were lines with directives for the compiler's preprocessor (those beginning by #). Then there were lines that began the declaration of a function (in this case, the main function) and, finally lines with statements (like the insertion into cout), which were all included within the block delimited by the braces ({}) of the main function.

    The program has been structured in different lines in order to be more readable, but in C++, we do not have strict rules on how to separate instructions in different lines. For example, instead of



    Code:
    1 int main ()
    2 {
    3   cout << " Hello World!";
    4   return 0;
    5 }

    We Could have written:

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

    All in just one line and this would have had exactly the same meaning as the previous code.


    In C++, the separation between statements is specified with an ending semicolon ( at the end of each one, so the separation in different code lines does not matter at all for this purpose. We can write many statements per line or write a single statement that takes many code lines. The division of code in different lines serves only to make it more legible and schematic for the humans that may read it.

    Let us add an additional instruction to our first program:




    Code:
    1 // my second program in C++
    2
    3 #include <iostream>
    4
    5 using namespace std;
    6
    7 int main ()
    8 {
    9   cout << "Hello World! ";
    10  cout << "I'm a C++ program";
    11  return 0;
    12 }

    In this case, we performed two insertions into cout in two different statements. Once again, the separation in different lines of code has been done just to give greater readability to the program, since main could have been perfectly valid defined this way:

    Code:
    int main () { cout << " Hello World! "; cout << " I'm a C++ program "; return 0; }

    We were also free to divide the code into more lines if we considered it more convenient:


    Code:
    1 int main ()
    2 {
    3  cout <<
    4   "Hello World!";
    5  cout
    6    << "I'm a C++ program";
    7  return 0;
    8 }
    And the result would again have been exactly the same as in the previous examples.

    Preprocessor directives (those that begin by #) are out of this general rule since they are not statements. They are lines read and processed by the preprocessor and do not produce any code by themselves. Preprocessor directives must be specified in their own line and do not have to end with a semicolon (.



    Comments

    Comments are parts of the source code disregarded by the compiler. They simply do nothing. Their purpose is only to allow the programmer to insert notes or descriptions embedded within the source code.

    C++ supports two ways to insert comments:



    Code:
    1 // line comment
    2 /* block comment */


    The first of them, known as line comment, discards everything from where the pair of slash signs (//) is found up to the end of that same line. The second one, known as block comment, discards everything between the /* characters and the first appearance of the */ characters, with the possibility of including more than one line.
    We are going to add comments to our second program:



    Code:
    1 /* my second program in C++
    2   with more comments */
    3
    4 #include <iostream>
    5 using namespace std;
    6
    7 int main ()
    8 {
    9  cout << "Hello World! ";     // prints Hello World!
    10  cout << "I'm a C++ program"; // prints I'm a C++ program
    11  return 0;
    12 }


    Run it See what happens.. Thank's me if i helped you understand C++ a little better..

  2. #2
    Dark_Goliath's Avatar
    Join Date
    Nov 2009
    Gender
    male
    Location
    Romania
    Posts
    308
    Reputation
    10
    Thanks
    28
    My Mood
    Sad
    Nice tutorial man very good tut for the begginers but u leched them i don't think u made tis tut by yourself
    Edit but u need a
    Code:
    system("pause");
    because if u don't have it the program will flash very very quicly on your scrren and u will can't see anything so add a
    Code:
    system("pause");
    before
    Code:
    return 0;
    And here you are your first program in c++
    Last edited by Dark_Goliath; 11-09-2009 at 02:13 PM.

  3. #3
    lalakijilp's Avatar
    Join Date
    Jan 2008
    Gender
    male
    Posts
    310
    Reputation
    9
    Thanks
    53
    My Mood
    Blah

  4. #4
    zeco's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    Canada
    Posts
    683
    Reputation
    12
    Thanks
    78
    My Mood
    Cynical
    He leeched it from Structure of a program I had thought it was a little fishy but i wouldn't have checked if you didn't bring it up. It's a good thing that Google can find virtually anything that is leeched.

  5. #5
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    Quote Originally Posted by Dark_Goliath View Post
    because if u don't have it the program will flash very very quicly on your scrren and u will can't see anything
    ever heard of cmd.exe? thats what console projects are made for..
    Ah we-a blaze the fyah, make it bun dem!

  6. The Following User Says Thank You to Hell_Demon For This Useful Post:

    B1ackAnge1 (11-09-2009)

  7. #6
    B1ackAnge1's Avatar
    Join Date
    Aug 2009
    Gender
    male
    Posts
    455
    Reputation
    74
    Thanks
    344
    My Mood
    Cynical

  8. #7
    B1ackAnge1's Avatar
    Join Date
    Aug 2009
    Gender
    male
    Posts
    455
    Reputation
    74
    Thanks
    344
    My Mood
    Cynical
    Quote Originally Posted by Hell_Demon View Post
    ever heard of cmd.exe? thats what console projects are made for..
    Noone apparently has... so instead of using the program as designed they cover up the symptoms instead

  9. #8
    zeco's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    Canada
    Posts
    683
    Reputation
    12
    Thanks
    78
    My Mood
    Cynical
    Man oh man.... This kind of leeching should be a bannable offense (Plagiarism. Terrible, terrible offense in academia).

    Sure code leeching is generally bad and technically plagiarism also, but usually you can't just post a link to the source code like you can tutorials like above. But i'm just saying that cause i like to look through source code a lot and the majority of it on MPGH is probably leeched =D ( I know im a terrible person ).

    Well what i mean to say is the leeching is only kind of ok when proper references are made to the original author.

    Oh and i've heard of CMD! I use it all the time! Wait.... are there actually programmers who have never used a command prompt? Even like graphic artists have to get down and dirty with command prompts sometimes!

  10. #9
    headsup's Avatar
    Join Date
    Apr 2009
    Gender
    male
    Location
    Pa
    Posts
    1,232
    Reputation
    8
    Thanks
    208
    My Mood
    Cynical
    Every1 stfu i did not claim this as mine..... Not at all i said this was mine u fools.....

  11. #10
    zeco's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    Canada
    Posts
    683
    Reputation
    12
    Thanks
    78
    My Mood
    Cynical
    Yeah but you also didn't tell us who's it was, which is just as bad =(.

  12. The Following User Says Thank You to zeco For This Useful Post:

    why06 (11-09-2009)

  13. #11
    Matrix_NEO006's Avatar
    Join Date
    Feb 2008
    Gender
    male
    Posts
    240
    Reputation
    12
    Thanks
    33
    My Mood
    Lonely
    if i see this guy in street i will dick slap him we got whole lot of C++/Memory hacking tuts why the fuck u post noobish tuts man.

Similar Threads

  1. New richest man in the world
    By Obama in forum General
    Replies: 39
    Last Post: 03-11-2010, 03:33 PM
  2. Hello World Disassembly
    By why06 in forum Assembly
    Replies: 15
    Last Post: 01-21-2010, 09:21 AM
  3. Hello World Anyone?
    By B1ackAnge1 in forum Assembly
    Replies: 11
    Last Post: 11-09-2009, 01:14 PM
  4. A closer look at Hello World App.
    By headsup in forum Java
    Replies: 5
    Last Post: 10-24-2009, 12:25 AM
  5. [C++]Hello World; Your first C++ Program
    By Mr. Bond in forum Programming Tutorials
    Replies: 3
    Last Post: 02-09-2009, 08:53 AM

Tags for this Thread