[Beginner's C++ Tutorial] Best around, I promise

c, c++, php, html, sql ..etc discussion. also includes codes and source codes.
Post Reply
ronokae
Allie
Allie
Posts: 83
Joined: Mon Jan 03, 2011 10:01 pm

[Beginner's C++ Tutorial] Best around, I promise

Post by ronokae »

Well I got pretty damn sick of scanning over C++ tutorials for days on end when I was a beginner,
they explained things kind of well, but I like to not just write code, but know Why I'm writing it,
and what the parts of the code do, thereby informing me how different parts of code can be used for multiple
purposes.

Code: Select all

So here you go Your Best C++ Tutorial ever ^.^
First you need a C++ compiler
once you have the compiler we'll begin.
However, if you just wanna quick skim, be my guest.
And if you don't wanna read a lot (WHICH I HIGHLY RECOMMEND it has LOADS of detail not the superflous[useless] kind either)
Skip down to the bottom.

GETTING THE C++ COMPILER [It's free, not crappy, and official]
go to codeblocks.org
go to downloads,
click on :download binary release then Choose your operating system(OS)
download the following:
codeblocks-(whatever version)-setup.exe(program/ide, whatever you wanna call it.)
then
codeblocks-(whatever version)mingw-setup.exe(compiler)

Start the program
File>New>Project>console applicaton(select C++)>title it>next>finish
to the left you should see a tree(not the plant but like a file tree):Workspace>your project name>sources
click the "sources" then click the main.cpp it will have a hello world program typed in already for yah.(atleast in the version i have)
so now all you have to do is understand this, if it doesn't have it typed in already it's cool you'll be able to do it on your own after this.
when your ready to run your program there should be a button somewhere on your thing that says "Debug" find it. it changes depending on what you use.


CODE

Code: Select all

#include <iostream>
using namespace std;
int main ()
{
  cout << "Hello World!"<<endl;
  return 0;
}
[/b]
Now normally this would be explained from top to bottom in a monotone by your teacher or in a complicated, unintelligible manner by other forum posts/web pages.
(though some are pure ownage), however I am not going do that.
I'm gonna get to the part that you may likely have your eyes focused on, and care about more than the rest of the text

"cout << "Hello World!" <<endl;"

That my friend is a statement. basically a line of words that has some effect on your program and what it does.
ANY line of code(not including comments which we will get to) in your C++ program is a statement.
The words in the statement may have different effects/labels, but nevertheless it's a statement.
just like "good morning" and "go to hell" are statements,
they have different meanings, spellings, verbs, etc, but are still called statements/sentences.

As for "cout<<"Hello world!"<<endl;"
this statement happens to also be the output of your program.
You see,
when you compile this program (transform the "pseudo-code (human readable text) to binary code (machine instructions)"
The words "Hello World" will be placed on a big sexy black console screen for you.

How will this be done?
Your text "Hello World!" will print for one reason and one reason only.... You are a superstar. Really you are.
That, and because of the <iostream>, namespace std, cout<<, and main() body that you've seen previously.

Review of these parts
The iostream, is a file that holds in it the basic C++ input-output library, not THE C++ library, only the standard input-output part.
(I've always thought of it as :io = input/output and stream = stream Therefore iostream = input/output stream.)

Well how does the computer know to use this file?
Because you typed #include (<-- That's called a preprocessor directive),
What it basically does is go to the "Don" of your program and say:Hey I have a "job" that needs to be done, but I need toinclude one of your guys to do it. when you figure out the guy you need just type'em in here: "<>".

Cool so now it's all falling in place right?
You told your program, "Hey whattup, i'm making a program, so #include <iostream> because I'll need an input-output stream for this to work, because i'll have some output/(maybe input) later, but not in this case)"
and your computer said "Sure just use me like you always do for your porn and your gaming and all that other crap! you never just sit down an say that you love me!" but it says it silently for otherwise it knows you will Nerd rage it into submission.
Anyway, #include is a preprocessor directive <iostream> is one of c++'s many standard files, we've been over this
but we need to know what it's called when they are put together, not individually.

So what are they together? #include <iostream>?,
it's a header!
basically a statement that gives the program a preprocessor directive, and tells the directive where it will be directed.
(to the input output thingy in this case. COol Story right?)
SO not only is this used for text, but it's used in programs to allow the user to enter letters, words, and numbers
and allow the computer to give output.
basically this thing will be used in loads of programs.

Continuing:
Now that we know what <iostream> and its buddy #include are we can discuss the
"using namespace std"
lets do this word by word.
using:what we're doing, "using" something this is just another word, what comes AFTER it is important
namespace std: This is difficult. First off, the "std" is just the name of the namespace we'll be using, in this case std=standard which means we'll be using the C++ standard library, (not just the <iostream>'s input output part which is actually harder to work with without using namespace std)
Now for the namespace:
I've looked at wikis, and many sources, and to the best of my understanding here is what i call a namespace;
A school. it allows many things to be part of one but have sub-sections.
In Human Style
for instance :Education, this would be a namespace, for it can be broken down like so

Education
History:(any country)-Human-world
Science:Math-Philosophy-Physics
Art:Painting-Music-Dance
Mystic:Sorcery-Universal understanding-Chakras, etc
And you can continue to break this down.
so if you're talking to your friend you won't say "Are you into the Education of the Art of Dance?"
You'll say "Are you into the art of dance OR dance?' because the namespace holds "Education"

Think of the C++ standard library. To humans this "library" is where you hold all knowledge that you have currently, without anyone giving you any extra information, just basic things you already know, like red, blue etc.
While in C++ it holds adjectives like int, double, char, etc.

In Computer Style
Rember the "iostream" being part of the library and managing he input output streams?
So okay take your hello world program for instance
WITH namespace

Code: Select all

#include <iostream>
using namespace std;
int main ()
{
  cout << "Hello World!"<<endl;
  return 0;
}
You have the iostream included, your using your standard library through the namespace,
and you've filled in your main body(we'll get to bodies and it will be brief).
everything runs smooth and you didn't do that much typing. because the namespace holds the standard library and the iostream
holds the input output part of the standard library.

WITHOUT NAMESPACE

Code: Select all

#include <iostream>
using std::cout;
using std::endl;
int main ()
{
  cout << "Hello World!"<<endl;
  return 0;
}
What the hell happened here? why the 2 extra lines? well you see, you included the output-input library section, but thats all, not everything else in it. so it still needs to call the standard library for some reason. what's that reason? to use cout and endl.
so now you have to say "use this guy too for the job he's from the standard library" hence: using std::cout;.
Remember there are many types of namespaces, you can make your own too.

MOVING ON FINALLY
the words "cout", and "endl" belong to the input, output library.
the "cout" is an object that is saying "this is gonna be output". hence:the extractors "<<" are pointing out
with "Cin" (requesting input) it would be ">>"
and the endl; just means End line, jump down after whatever is typed. (if you don't like typing <<endl; you can always type \n within the code: cout<<"Hello World!\n"; NOTE: the \n actually does 2 things. you see when C++ sees the quotes after your cout<< it says okay so everything in these quotes are just stuff im putting on screen [output] so nothing in here is important. but when it sees that "\" it says "Damnit i'm being used again" the "\" means the next character is special so the c++ briefly jumps out of the 'read in the quotes' mode and does a fast command, which is "n" or "newline")
Oh i never explained what the ";" semicolon was for.
You guys are gonna hate this but you'll learn to love it, the semicolon is basically put at the end of most the commands you give your program.
Get into the habit of typing it in. it'll mainly be in your bodies. Which we'll get to.

[u]BODIES[/u]
Naked and beautiful, the way they all should be.
your program can have a few bodies.
all bodies do are hold commands and stuff
so your body main()
is gonna be the main body of your program and do the stuff in it
all bodies have a name, have (), and a type (int, void, string, whatever).
oh by the way you body must open and close this "{ }" those curly braces.

int main()
{
do stuff;
} = WIN
finally the return 0; and the overview.
the return 0 basically returns 0 at the end of your program execution. meaning basically everything else ran well.
if your making a c++ program like this in visual studio or anything else
type getchar(); before return 0;
because it's likely that you run your program or debug it and then it just gives a black screen then vanishes
that means it ran fine but computers do EXACTLY what you tell them
you told it to run, say hello, then leave, not stay so you can read it.
the getchar(); will just make it stand still until you press enter.

NOTE:Comments,
comments in a C++ program are just notes you put there for your self, or people
who may run across your awesome program in a Neo-Apocalyptic society years from now
explaining what your program does, is gonna do, does at line X, whatever.
There are 2 ways to write comments: with "//" or with "/* */"
I will display both in this example.


EXAMPLE & Overview!!!!! (congrats)

Code: Select all

#include <iostream> // <----Preprocessor directive
using namespace std; //comments do not Effect your programs behaviour they are disregarded lines.
int main () /* <--- Main body, (you can also write comments like how this one is written) */
{
  cout << "Hello World!"<<endl;  //Command, output Hello World! then endline, (all commands end with ";")
  getchar(); //wait for user to press enter before exiting and returning 0;
  return 0; //return 0 after all is complete then exit
}
Remember, C++ or any programming language is all about logic and creativity.
creatively do what you must, or solve any program, in a straightforward logical manner
start with the foundation then build over top of it.
To build on your new foundation of this language Search forums here and use (http://msdn.microsoft.com/en-us/library ... S.85).aspx) look through its library for reference to further commands & examples.
Search Sethioz for more info on Computers even game hacking/WEP cracking.

Good Luck.
User avatar
MagicalSilence
Special
Special
Posts: 289
Joined: Mon Sep 21, 2009 11:06 pm
Custom: Retired :3
Location: Santa's Workshop

Re: [Beginner's C++ Tutorial] Best around, I promise

Post by MagicalSilence »

Thanks i find it useful been trying to bother to learn C++ but haven't seen good examples to get it started.
Post Reply