It appears you have not yet registered with our community. To register please click here.

Origin XT RPG Network Home







XT Project Introduction
Blah blah intro to this place. To be composed later.

C++ - Tutorial, Lesson 1, Introduction to the Basics


Mar 6 2007, 04:48 AM (Post #1)
Well why can't we do the shuffle?!
* * * * * *
Posts: 1,679
Cash: 183,455 / 0
Group: Nobility
Joined: 7/12/03 08:49 PM

Lesson 1 - Introduction to the Basics


Hey guys, this is Nesticles here! I've been taking C++ for a while now in college, and there is alot I know about it. Granted, there is alot I do not know about it, but I can at least give you some pretty decent tutorials that will cover most of the topics covered by a standard C++ book. Hopefully I can explain this wonderful and powerful language in simple enough terms for even a small child to understand and enjoy.

Introduction


The first thing you should do if you wish to code in C++ is get a compiler! Visual C++ is a great compiler, however, the full version of it does cost money. Microsoft does offer a watered down version of it for free, though. However, I usually use Dev-C++, which is a small compiler that gets the job done. Best of all, it's free.

So, once you've got that installed on your system, run the program, close the tip menu, hit the file option in the upper left corner, go to new, then go to source file. This is a tutorial, so I find that it doesn't make much sense to be creating separate projects for each lesson we do, I'll save you a little bit of space on your HD.

You will notice that the compiler opens up a blank file for us to work on, much like notepad. However, unlike languages like PHP, you cannot use Notepad to make C++ programs (.exe's if you're on Windows. Something interesting about Linux is that a C++ compiler does not need to give compiled source code an extension, it's permissions, however, must be set to executable.)

So why do we need a compiler for C++ and not PHP? Well, that answer is rather simple. PHP is a web based language. A request to a PHP script is rendered by the server's PHP module and interpreted, then sent back to the browser. In a way, the PHP server is a little like a compiler, but it does not do the same thing as a C++ compiler; hence, PHP is called a Hypertext Preprocessor.

When you compile source code in a C++ compiler, at least in Windows, it will make an .exe file or an executable file. Unless you use advanced Windows programming in C++, or use C#, all of your programs will be plain, MS-DOS console applications. But what does a compiler do besides that? Well, the special thing about a C++ compiler is that it reads the C++ source code, interprets it, and sends it back to the computer in the only language it can understand...Binary. For those of you who do not know what binary is, it is machine language that is comprised of only two numbers, 0 and 1. This is the only thing the computer can actually understand. All of the video games you play and program you run are actually comprised of nothing more than 0's and 1's which can represent characters, decimal numbers, and all sorts of other things. But why 0's and 1's? Well, the computer can only understand two commands, off and on. 0 being off and 1 being on. You see that blinking light on your computer panel everytime the computer is loading something? That is a perfect example.

Now that we have that down, maybe we can get into some coding, eh? Here is our first example.

Example One


CODE

#include <iostream>
using namespace std;

int main()
{
cout<<"Hello world!"<<endl;

system ("PAUSE");
return 0;
}

Here is our first, simple program. Copy and paste it into your compiler of choice and hit the compile and run button. If you're using Dev-C++ like I am, that button should look like a multi-colored window. There are a set of them in the upper left corner, and the button we're looking for is the third one from the left. The result you see should look like this in the MS-DOS window:
CODE

Hello world!
Press any key to continue...

To close the program, just hit any key like it says. Now I bet you're wondering why this happens, and what this code means exactly. Well, I'll explain it line by line.

#include <iostream> - This line includes the library <iostream>, which houses a number of very useful functions in C++ and is the most widely used library for obvious reasons. You may include many other library functions, including ones you make yourself! However, if you do make your own library's, which will be discussed later on, the code would look like #include "mylibrary". Most people use header files, which can be incorporated as part of a project and look like this: #include "myheader.h"

using namespace std - This means that the program is going to be using the "standard namespace", which includes more helpful functions that are commonly used in C++ programs.

int main() - This is the main function, and is the center of the program. Everytime a C++ program is compiled, the code within the main function will be read FIRST. You may create your own functions later, and that will also be explained. Even if your homemade functions are declared before int main() in the program, int main() will still be read first. Int main() can also do stuff with the command line, which we will also go over later.

cout<<"Hello World!"<<endl; - Cout is a function used to display text on a screen. The arrows are important here, do not forget them. "Hello World!" is the text displayed on the screen. Don't forget the arrows after the text, because if you wish cout to display more than one thing on the screen, you have to use the arrows to separate those items. Finally, endl will make the line end, so if you cout again that line will be below this one. It's like hitting the enter key in a word processor to start a new line. Don't forget the semi-colon at the end of the line, this is very important! A semi-colon should end nearly every line of C++ code.

system ("PAUSE"); - This makes the system pause the program to await a key hit by the user which will then make the program read the next line of code. I put this here so you could actually read the text that the program output on the screen. Without this line of code, the program would just exit before you even knew what happened.

return 0; - This returns the value of 0 to the computer, and in C++ if 0 is used as a return value then it is known as "true". Therefore, the program executed successfully and exited successfully. -1 would mean false, and there was therefore an error. return EXIT_SUCCESS; has the same effect as return 0, but it's easier to just use return 0;

If you've never programmed in JAVA, then don't worry about this, but for all of you JAVA programmers, this is probably very different from what you're used to. JAVA is a completely OOP (Object Oriented Programming) language, so therefore all of the code is within a class function. However, C++ does not require that everything be within a class function, although C++ supports OOP just as well as JAVA does.

Getting back on track, it's important for every programmer, not just C++ programmers to comment his code so it can be easily read by himself and by others, in case you go open source (sourceforge rules!).

So let's comment the above code!

Example Two


CODE

#include <iostream> //our uber awesome library
using namespace std; //the standard namespace

int main() //the main function that gets executed first in the program
{
cout<<"Hello world!"<<endl; //displays hello world on a screen then ends the line

system ("PAUSE"); //pauses the system
return 0; //returns a good or "true" value
}

Notice the two back slashes before the comments. This lets the compiler know that these are comments and not part of the actual code, and therefore they will not be compiled with the program. You can also use this to comment:
CODE

/* This allows me to make a huge block of comments
So I don't have to keep using back slashes
For several lines of comment
Because that would be pointless and a waste of time
*/

Afterword


That completes Lesson 1 of the tutorial. Congratulations! You're well on your way to becoming a C++ programmer!

Next Lesson: Lesson 2 - User Input and Variables

~Nesticles~
Post Options

 
Mar 6 2007, 07:16 AM (Post #2)
Not Odd anymore
* * * * * * * * * *
Posts: 45,875
Cash: 1,915,578 / 1,817,041,051
Group: Administrator
Joined: 7/10/02 09:48 PM
Very nice lesson. There's just one note I want to make: try using [code] and [codebox]. I think they'll make your code look more clean and organized.

Just a thought ssmile.gif
Post Options

Mar 6 2007, 03:55 PM (Post #3)
Well why can't we do the shuffle?!
* * * * * *
Posts: 1,679
Cash: 183,455 / 0
Group: Nobility
Joined: 7/12/03 08:49 PM
All taken care of, thanks for the tip.

This post has been edited by Nesticles: Mar 6 2007, 03:59 PM
Post Options

Mar 6 2007, 11:56 PM (Post #4)
Not Odd anymore
* * * * * * * * * *
Posts: 45,875
Cash: 1,915,578 / 1,817,041,051
Group: Administrator
Joined: 7/10/02 09:48 PM
No problem sbiggrin.gif. A couple more things though:

1. I suggest creating a new topic for each lesson, but title them so that, when listed alphabetically, they are ordered correctly. For example: "C++ Tutorial: Lesson 1 -- Introduction to Basics", "C++ Tutorial: Lesson 2 -- User Input and Variables", etc. That'd make everything neater, and easier for us to comment. (Plus you get more zeny stongue.gif). If you want, I will split this into two topics.

2. I don't mean to be picky, but headings and stylizing may help people understand things better, such as [b] tags. You can bold headings, and for some cases, (such as the transition from OOP to commenting), create headings. I think it'll look much neater and more organized if you do that.

3. Is there a separate library that needs to be included for system() functions? For C I gotta include stdlib.h.
Post Options

Mar 7 2007, 03:49 AM (Post #5)
Well why can't we do the shuffle?!
* * * * * *
Posts: 1,679
Cash: 183,455 / 0
Group: Nobility
Joined: 7/12/03 08:49 PM
Phew, let me answer some stuff here.

1. Will do, sure you can split this into separate topics, I didn't really know how you wanted it so I made the lessons in one topic.

2. Maybe I will use that stuff, it seems like a good idea to me.

3. Not in C++, my friend.
Post Options

Mar 7 2007, 04:47 AM (Post #6)
Not Odd anymore
* * * * * * * * * *
Posts: 45,875
Cash: 1,915,578 / 1,817,041,051
Group: Administrator
Joined: 7/10/02 09:48 PM
1. ssmile.gif Will do.

2. Cool.

3. Oh, thanks for the info. I guess system functions are internal
Post Options