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.
- 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 damn 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.
**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>>

