Advertising (This ad goes away for registered users. You can Login or Register)

Learning Programming With C++ (III)

Discuss about your favorite (gaming...or not) devices here. The most popular ones will end up getting their own categories
Programming discussions for your favorite Device
Forum rules
Forum rule Nº 15 is strictly enforced in this subforum.
Post Reply
User avatar
SHA-1
Posts: 26
Joined: Sat Jun 09, 2012 9:20 am
Location: In my house...

Learning Programming With C++ (III)

Post by SHA-1 » Wed Aug 15, 2012 10:39 am

<<Prev | Next>>

Explaining our first C++ program

In this part, I will explain the program foo.cpp that we created in the first part of the program.

Code: Select all

#include <iostream>

using namespace std;

int main()
{
    cout<<"Hello World!!!"<<endl;
    return 0;
}
*Note to windows users: cin.get(); will be needed after cout<<"Hello World"<<endl;

I will try to keep this part simple so I won't go all uber geek on you. :ugeek:

Code: Select all

#include <iostream>
If you've read through part II(If you didn't go back), you'll know that this is a preprocessor directive and that it include essential libraries/makes code easier to handle.

The io in iostream means input / output, so this include directive is essential if you want to display some text on the screen / get some input from the keyboard.

The next line:

Code: Select all

using namespace std;
This line is not really essential but makes writing code a MUCH quicker.

Let me give you an example:

If I didn't include

Code: Select all

using namespace std;
The hello world will be like this instead:

Code: Select all

#include <iostream>

int main()
{
    std::cout<<"Hello World!!!"<<std::endl;
    return 0;
}
And having lots of cout's and endl's and having to write std:: in front of it every time is just dang annoying so that is why we include that line.

The next line is:

Code: Select all

int main()
This line comes from the programming language C and is function declaration. A function is a place were you can group code and make it do a specific task.

So as you can see we are declaring a function called int main() and it has no arguments inside the parenthesis and it returns an int.

The int in the int main() means integer, and if you don't know a integer is, it's a whole number (no decimals).
The reason the main() function is a int is because we have to return a value at the end of the function.

The main() is a very special function in which it is the function where everything in it is executed and only it executed.

The main() function is a MUST!!!
If there is no main, the program will have to execute thus giving us an error.

Code: Select all

{
The function starts here, so everything inside the { is part of main().

Code: Select all

cout<<"Hello world!!!"<<endl;
Remember when I was talking about input / output, well cout is a way to, you guessed it, output text which in this case is Hello World!!!

Now, cout is a way to print out a character string to standard output.
A character string is surrounded by double quotes ("") in C++/C.

Another cool thing about it is that it accepts formatting which are special characters used to format the look or the presentation of the text.

endl is a way of saying "newline" and that is also flushes the buffer (writes unwritten character in the buffer to the output).
You can achieve a newline by writing '\n', but because endl flushes the buffer it's best to use endl then '\n'.

Now, if you look closely you can see a ;, this indicates to the C++ compiler that the line of code is finished, so DON'T FORGET IT!!!
Otherwise it will give you a bunch of errors. :shock:

**For windows users, Linux users please skip.

Code: Select all

cin.get();
What this does is makes the program wait until the user presses 'ENTER' key.

Code: Select all

return 0;
Remember when I said that we have to return a value in main(), well what this does is return the value of 0 to the main() function and marks the end of the function. The value that the return must match the one declred in the function (i.e int main())

This ends our program, giving the value from return to the OS. On POSIX machines 0 means the program has ended with no problems, while returning the value of 1 means that the program has ended with errors.

***Note: In C++ you dont actully need a return 0; the compiler adds the return 0; if there is none found.

}

This means that the function has ended.

Now, deep breath in, deep breath out.
Was that so hard ?

<<Prev | Next>>
Advertising
Last edited by SHA-1 on Thu Aug 16, 2012 9:40 am, edited 1 time in total.
Languages, I know:
C++, C, Perl, Bash.

Languages I'm learning:
Assembly, Python, Java.

User avatar
Acid_Snake
Retired Mod
Posts: 3099
Joined: Tue May 01, 2012 11:32 am
Location: Behind you!

Re: Learning Programming With C++ (III)

Post by Acid_Snake » Wed Aug 15, 2012 9:05 pm

I'm gonna be paying close attention to your tutorials, seeing how this and c look too much alike I might learn both at the same time.
Advertising

Davee
Guru
Posts: 278
Joined: Mon Jan 10, 2011 1:24 am

Re: Learning Programming With C++ (III)

Post by Davee » Wed Aug 15, 2012 9:31 pm

Code: Select all

using namespace std;
Should probably mention the downside of this, std namespace covers a lot after all.
Follow me on twitter: @DaveeFTW

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: Learning Programming With C++ (III)

Post by m0skit0 » Sat Aug 18, 2012 7:57 pm

Just what I was going to say Davee. Having your program include std can lead to severe headaches later. The std:: typing is worth it :lol:
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

User avatar
SHA-1
Posts: 26
Joined: Sat Jun 09, 2012 9:20 am
Location: In my house...

Re: Learning Programming With C++ (III)

Post by SHA-1 » Sun Sep 02, 2012 9:19 am

m0skit0 wrote:Just what I was going to say Davee. Having your program include std can lead to severe headaches later. The std:: typing is worth it :lol:
I was going to explain the downside after a couple of parts and when the reader has a better understanding of C++.

But I appreciate your suggestion m0skit0 and Davee. :D
Languages, I know:
C++, C, Perl, Bash.

Languages I'm learning:
Assembly, Python, Java.

Post Reply

Return to “Programming and Security”