Gw Temp

Menu

Tutorial - 'C++ For Beginners' by Quest-Master

An item about Programming Languages posted on

Blurb

Want to start out on C++? Then read this tutorial!

Body

Yes, to tell the truth, I have seen a lot of C++ tutorials around. However, many of them don't cover what I see to be critical to starting off with C++, so I am writing this tutorial at the moment. Before we start, here are some things I will be covering: software, printing on the screen, variables, and strings. Let's being with the materials needed to make C++ programs ^__^
--
C++ can be written in any text-based program, something even as mediocre as notepad. If you want to get a bit fancy though, with a full-blown IDE that has features and other things, you'll want to continue reading. What's the difference? An IDE: integrated-development-environment. A compiler: basic program usually run through a console window that converts a .cpp file into a executable (a program other people can open and work with, not just code). Also, most IDEs have the compiler built into them, so you don't need to worry about things of the sort. You'll be using IDEs though most likely, no plain console compilers. The best free IDE out there I can recommend to you is Dev-C++ by Bloodshed, and the address is: http://www.bloodshed.net/devcpp.html
Visual-C++ by Microsoft though is much better and used by professionals, but has a bigger price tag-- estimated at about $109 (US). So most people here will be using Dev-C++.
-----
Let's get cracking now on the coding. In your notepad, or IDE, whatever, create a new project/file. If you're having trouble with your IDE, you can make a comment about it and wait to have it answered, but this isn't a tutorial on IDEs, so I won't cover all of them. Anyhow, back to the coding. Let's start off with this:

#include <iostream.h>

int main() {
cout << "Hello world!";
while(1) { }
return 0;
}

Now I shall explain all of this too you :D
-- #include
This is a header file, something which contains functions you need to do certain things. We are calling this because it contains functions needed for input and output.
--int main() {
Int is a data-type, something in C++ which I will cover in a part two of this tutorial. main() is the function which is the core of your program and the starting point of it. I just added the { in the first line to save room. And so you know, everything of a "function" (set of directions you make that do something) is enclosed in a { } followed by the function name, which always has a () at the end.
--cout << "Hello world!";
cout is a function which prints something onto the screen. << means shift left, which is used by cout to receive the string. A string is simply a length of words/directions/etc. created by you. The string cout is using here is "Hello world!" which is finished off by a ;. Every line in C++ should be ended with a ; besides: #include lines, first lines of functions (like int main() {), and few other instances.
--while(1) { }
Many tutorials don't cover this function, but I do because in many IDEs/compilers what happens is that the program you made comes up for a second and then closes. while(1) { } keeps it open until the user exits. This is how it works; the while() function is when a process is carried through while an expression is true, or false. What is an expression? It is a line of statements, such as 5 + 2 = a, or something like that. These will be covered more later on. while(1) keeps your window open, because 1 means true, and what its doing is while its true (which is forever) it won't do anything (except keeping the window open).
--return 0;
This simply tells the program how to terminate properly, but is used in other functions to return a value.
--}
And the function and whole program is finished.
-----
Compile this, and it should work and open a console window that reads "Hello World!" Interesting, eh? Could've just written it, but it gets us started with C++.
-----
Let's do a bit more now:

#include <iostream.h>
int a = 1337;

int main() {
cout << a;
while(1) { }
return 0;
}

Explanation time!
-----
(Note: I'm not reviewing what has already been reviewed)
--int a = 1337;
Here, we are creating a variable that has an int datatype. Don't worry about the whole datatype thing right now ^^; We have created a variable called a, and it equals 1337. That shouldn't be too hard.
--cout << a;
I've covered cout, but haven't with variables. Instead of adding ""s outside of the a, I've left it by itself. When variables are being printed, you leave them alone. Only strings are quotation-ed. :P
-----
Compile it and you should see 1337 printed on the console.
-----
Now let's make it so that a string is printed, but we are still using a to print it.

#include <iostream.h>

char a[12] = "I know C++!";

int main() {
cout << a;
while(1) { }
return 0;
}
-----
The same thing has been done here that was done in the previous script, but we printed a sentence instead of number. Notice how there are ""s that enclose the string.
-----

Enjoy it? Hate it? Leave your comments here. I hope you learned something out of this anyhow. I'll be making a part II to this soon, covering operators, and more advanced concepts in C++. Thanks for reading it, and give me a call if you need any help.