[BEGINNERS TUT] 'Hello world' C++

Discussion in 'Programming' started by Da_Purr, Jul 14, 2007.

  1. #1
    Well, I noticed there aren't many C++ basic principles tut's out there. So I'll start with a simple 'Hello World' tut and if you like it, I'll add more tuts.

    This is meant for all the beginner programmers, if you want some advanced tut's please post it in this topic.

    OK, I'll provide code first and then I'll explain it.
    Compiled and tested with Dev C++ :

    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        cout << "Hello World !\n";
        
        system("pause");
    }
    Code (markup):
    
    #include <iostream>
    
    Code (markup):
    OK, this block simply explains to include the <iostream> library, which stores some functions, including 'cout'. Yeah, it's a pain in the ass if you're previous language was VB 'cause you didn't need to include library's.
    Up to the next code:

    
    using namespace std;
    
    Code (markup):
    All this tells you is that it uses the 'std' namespace. 'Why would I include this piece of code?' you say. Well it's optional, else you have to write 'std::' before EVERY function declared in the 'std' namespace. Btw, if you don't include the semicolon, your code won't work. And know the very last piece of code:
    
    int main()
    {
        cout << "Hello World !\n";
        
        system("pause");
    }
    
    Code (markup):
    Ah, the very heart of our program:)
    In C++, a console program must have a 'main' function. This is like the the heart of a console program. You can see every other function as an organ of the program (if it had one:p).
    With the 'cout' function you tell the program to print a variable or a string. In this case: 'Hello World !\n'.
    You've probably noticed the '<<'. This tells the program to output something. The '\n' is basically a newline.
    Try to run the program without the '\n' and you'll see that 'Press any key to continue . . .' will be on the same line as our string. At last, the final line of code, the 'system(pause);'.
    It tells the program to pause it's code and wait for the user to press a key, and then it'll resume it's activity.

    That's basically it for this tut:)
    Hope you learned something.

    - Da_Purr -
     
    Da_Purr, Jul 14, 2007 IP
  2. ProgrammersTalk

    ProgrammersTalk Peon

    Messages:
    684
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    this tutorial over and over again.. kind of headache lol..
     
    ProgrammersTalk, Jul 14, 2007 IP
  3. Da_Purr

    Da_Purr Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Lol, anyone a suggestion for a tut in C++ ?
     
    Da_Purr, Jul 14, 2007 IP
  4. Xabber

    Xabber Active Member

    Messages:
    437
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #4
    A less basic tutorial :p
     
    Xabber, Jul 14, 2007 IP
  5. Da_Purr

    Da_Purr Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Well, you gotta start somewhere, lol.
    I'll make a 'basic API' tut if that's oke.
     
    Da_Purr, Jul 14, 2007 IP