Jump to content
xisto Community
Sign in to follow this  
de4thpr00f

C++ Lesson 1: Introduction Tutorial to c++

Recommended Posts

This are not my tutorials, althought it's always nice to share. And i got the permition for these. (Thanks to GaianLurker for this)
Enjoy and try to keep up.

#include <iostream.h> // io = in/out stream.. I'll explain.int main(){	cout<<"Hello, Xisto."<<endl;   return 0;}
This is a very basic program. In fact, it's the program most tutorials start with in any programming language, so.. it's the one I'm starting with.
#include <iostream.h>

Let's start here. # is a signal used to say something should be done first. Yes, I know all the terminology, but I'm going to spare you. #include, #define, and others are to be done before your actual program. Right now, let's just deal with #include. Some of you may have seen Zero's work in vB. TONS of files come with his program, right? Well in C++, our dependant files get "included" in our main program. If you're familiar with DLLs, it's like that. The extension .h is for a header file. Most header files are really just where the compiler has taken several tasks that would be very difficult to code by hand, or are written in ASM for speed.. and make them easily accessible to you. iostream stands for in/out stream, which refers to accepting input, and expelling output. You may have noticed the comment syntax; //. Anything after // in a single line is commented out. This means that the compiler will ignore it and you may use the space to explain things for yourself and others. A good programmer leaves good comment. If you start out with 50:50 code:comments, you'll learn the proper balance as you learn to properly code.
int main(){}

Alright, let's ignore what's INSIDE our function for now. Anything you see that has () after it is a function. Functions are snippets of code that perform actions (usually, we'll assume they do for now). int stands for integer. Every function has to have a type. The types we will most likely deal with in these tutorials will be int, double, char, (maybe strings), and void. Right now let's focus with what we have. If something is an int function, it must return an integer. That's why we have the return 0; line. Hypothetically we could include THIS program could, for example, check if the fishing servers of Gaia Online are up. It could return 0 if all 3 connected successfully, or 1 if one of the servers failed. The other program could check if our main here returned 0 or 1. >.> Maybe I'm getting ahead of myself, so let's look at the syntax again.

type name() {}

Is a function's syntax. Every program must have a main(), which I'll explain when we get into scope (much later o.o).. Inside the ()'s we will put arguments, and inside the {}'s you put the code executed when the function is called.
cout<<"Hello, Xisto."<<endl; (alternatively cout<<"Hello, NEF.\n"; or many others)
Alright, let's start with the basics.

cout is a command that is defined in iostream.h .. and you can see the syntax here. When you want to display something to the command prompt, the command is cout<<"RAW TEXT"; Every line of code must end in a ; .. for now. There are exceptions, but you can almost guarantee if there's a line of code, it'll need a ; after it. cout is very special in that it can be used several times in one line without repeating the command. For example, you can type cout<<"Hello, "<<name<<". How are you?"; if "name" is a variable with their name. One variable that is always declared, unless you declare it and give it a value (please do NOT do this to reserved words, such as cout. You'll just shoot yourself in the foot.) would be our friend "endl". endl stands for end line, and will move your text down to the next line. Think of it as an enter space. If you are in raw text string, you can also use \n to do the same.
return 0;

We had to return a value for our int main(). Although most interpreters will consider this "understood" if you omit it.. it's good practice to make sure it's there, especially since when we get into REAL functions, they'll rarely return 0.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.