Hack Forums

Full Version: C++ Tutorial
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Welcome to my first C++ tutorial. Today, I'll just be teaching you about the Hello World "Program".

C++ isn't a simple language, but I'll try to cover as much as I can and make it as easy to understand
as I can.

Lets start out C++ file with some comments telling people what the file is about. For this tutorial, I
will be using Dev-C++ to write, compile and link my C++ files into a .exe. Before we start, lets
talk about the three things I mentioned there.

First, we need to write our file. This can be done in ANY text editor, so long as when you save you go
"All Files" and type in the C++ extension (.cpp) after the file name.

That takes us onto compiling. Our .cpp file is just a format of text file which can be COMPILED. When you
compile the .cpp file, you make it into a runnable executable (.exe). The compiler simply turns the code
into a displayable format.

And finally, the linking stage, could be called part of the compiling stage. What this does, is include any
files that are referred to during the code, so that you can have just ONE .exe, rather than having to send
someone multiple files, and tell them exactally where to put them (otherwise an error will be thrown because
the file could not be found in the described path).

That's just a brief introduction of what writing, compiling and linking is (*NOTE: Some programs do this
separately. Dev-C++ is good, because it does all 3).

As I said before, lets start with some comments. There are two types of comments in C++ coding. One is the
single line comment, and the other is the multi line comment. Single line comments start with a "//" (without quotes)
and anything after that will not be counted as C++ code. Multi line comments start with a "/*" (without quotes)
and end with a "*/" (without quotes). It is best to use multi line comments, as sometimes on one editor, it might
look like one line, but on another, it's multiple lines, and then the compiler will throw an error.

So we will be using multi line comments :

Quote: /* My First C++ Program
Made with Dev-C++ */

This code will be ignored, as it is inside the opening and closing combination of multi line comments.

With every "program" we make (almost certainly) you will need to include a certain file called the "iostream" (without quotes).
To do this, we need to use the following code (I will talk more about the iostream later) :

Quote: #include <iostream>

Here, we use the INCLUDE command, then tell the compiler what to include by enclosing the name in less than
and greater than signs.

Lets get down to the nitty gritty, the part where we actually print "Hello World" onto the console screen.
We have to use something called a namespace (a namespace is a reserved space inside the iostream.
It can perform certain functions for us.). The namespace we will be using is "std" which sends info to the console screen.

*NOTE: Before we go on, lets clear something up. Basically, we have something called a library, which contains
all the files we can INCLUDE, and those files contain all the namespaces we can use. Think of it as a diagram :



Hopefully you understand better now what we are accessing.

Just before we go onto the "echo/print" part, we need to specify something called "main". main is in fact an function, and the
"int" part specifies that the function will be returning an integer value (a whole number value).
We MUST have a main section otherwise an error is thrown (the main function is the starting point of our code. If there is no main,
when it comes to compiling, it will see no place where your code actually starts and will throw an erro.).
All our code goes inside the main function :

Quote: int main()
{

}

Now lets get onto the echo part. We have to use something called cout to send stuff to std which sends it and prints
it on the console window. But we have to refer to what namespace we are using before we can use something inside
it. To do that, we have to put the namespace name and then two colons (: after it, then what we want to access :

Quote: int main()
{
std::cout
}

That is how we access cout. Then we have to use two less than signs (<<) to send stuff to cout :

int main()
{
std::cout << "Hello World"
}

We can now either end our string with a semi-colon ( (You only end STATEMENTS with semi-colons. Including the iostream
was not a statement, which is why we do not need to put a semi-colon after it), or we can put to more less than signs and
type "endl" which is effectively pressing enter (so whatever you have next goes on a new line). But don't be so hasty yet! "endl" is also part of the std namespace and requires the same "std::"
Lets try that :
Quote:int main()
{
std::cout << "Hello World" << std::endl;
}

And we have now ended our statement with the semi-colon.

If we compile and run this now, you will only see a console window flash. For anyone who knows about batch, this is because
it executes the code, then shuts off. So we need to demand user input before we shut off. To do so, we simply use the function :

Quote: system("pause")

So :
Quote:int main()
{
std::cout << "Hello World" << std::endl;
system("pause");
}

And finally, we have to return an integer value (as main is an integer function). We will return "0" which is a way of saying the
program ended without a problem :

Quote: int main()
{
std::cout << "Hello World" << std::endl;
system("pause");

return 0;
}

Try compiling and running this. You should see the text "Hello World" displayed, and you must press a key before 0 is returned
(to say the program ended without a problem) and the program ends.



I did not write this I saw it read it looked at the comments and alot of people where saying it just another tutorial you stole so im sorry I can't give credits to the owner.
nice tutorial, i wish you wrote this 4 days ago when i started C++ would have saved me a lot of trouble /screaming
lol really jimmy?
This is really basic.
yea i know, sad. im using visual studio 2008 and i didnt do a "blank project" thing so i always got like 3,000 compiling errors
From the looks of this, it appears that you put a lot of time and effort into this.

FAIL.

I kid, i kid.

Very good job. I bookmarked this and I plan to read this later. :)

Edit: Awe, you didn't write this. :( I'll still read it though.
Awesome, I need to relearn my C++. I forgot it all awhile ago :(

Thanks :)
Excellent tutorial.
I am looking forward to more C++ tutorials from you.
I didn't write it guys read the bottom. I could of wrote it though its not very in depth.
Even if it was made by you,a lot of people will shout ..
Because you can find tons of Hello World tutorials.

Nice tutorial,straight to the point..
Pages: 1 2
Reference URL's