PDA

View Full Version : [C++] Hello World



Sdyess94
10-02-10, 11:44 AM
As always, we will start with a Hello World console application. This is seen to be many peoples first coding experience and will more than likely remain that way for quite some time. Let's get started.

Lets create the frame of the application:


#include <iostream>

void main()
{
using namespace std;
cout << "Hello World" << endl;
cin.get();
}

Unlike most hello world tutorials, we used void instead of int. I figured it was too early to have you ponder on returns.
So let's break this into small pieces.



#include <iostream>
This as you can obviously see, includes the iostream file.
The angle brackets (< and >) refer to a standard header which is part of the C++ library.
iostream suggests input/output instead of random access or other things.

Next, we have:


void main()
If you have read my java tutorial, you will somewhat understand this since Java is created from Smalltalk and C++.
void means the function doesnt have to return anything. Normally people use int main() in which they have to return an integer. we named the function main because the first thing a program does when it runs is search for the main function to run off of.



using namespace std;
Without this part, the script would look like this:


std::cout << "Hello World" << std::endl;
std::cin.get();
It pretty much just makes it simpler and less for you to read and make it more organized. This namespace isnt always used though, beware.



cout << "Hello World" << endl;
cin.get();
Final part here.


cout << "Hello World" << endl;
cout << is going to output whatever is after the "<<". For example, you could do


cout<< "roflcopters";
Your application will have an output of "roflcopters".
The second part, where it has <<endl, ends the line, and the next piece of output will begin on the next line. And then you use a semicolon(;) to end the statement.

Finally we have:


cin.get();
I've included to this to keep your application open. Without it, it would close.

This tell the console to get the next piece of input, which should be the enter key. Once you press that, it'll close.

Thanks for reading my tutorial on the Hello World application written in C++.

If you have any questions feel free to PM me.

Apple
10-02-10, 11:53 AM
nice one +rep & wb by the way

P1raten
14-02-10, 05:33 AM
#include <iostream>

void main()
{
using namespace std;
cout << "Hello World" << endl;
cin.get();
}



cin.get(); is not needed at all. If you use visual studio then just press ctrl + f5 to execute the application.

If you dont have visual studio, then i suggest that you use
system("pause"); instead.

mec
26-04-10, 04:02 AM
sdyess strikes again. not bad!

DeVTeo
17-02-11, 02:40 AM
This tutorial for beginners in C++ programming is good :p +rep