Macrosoft
2007-09-08 00:27:17
Hi, I am unsure of whether this belongs in the C++ section or the Tutorial Island section. If It does not belong here, please tell me or move it.
Please REPLY
Just Finished Massive Update, Enjoy!
Introduction
C++ is a highly powerful programming language used in many professions. There are many rumors that it is difficult. In my opinion, that is bs, though C++ looks scary at first, doing more advance things become much simpler than many other languages. Once you understand it, it will become extremely easy.
I may add a section on how to download a C++ compiler, but right now it is late and I have to go to bed :(
In this tut I will be using Microsoft Visual C++ 2005 as my compiler.
You can download it here
Creating a New Project
First, open Microsoft Visual C++ and go to File>New>Project
In the project types half (left), click Win32 in Visual C++
Then Click, in the right half, Win32 Console Application.
Name the project and click Ok
A window should apear called Win32 Application Wizard. Click next and click empty project in "Additional Options"
Also make sure that Console Application is checked, in the Application type
Now click finish.
Now we need to make a source to type our code in, in the project menu, click add new item. Choose code in the catigories half(left) and C++ file on the right. Name it and click "add"
Now you have a place to type your code.
Variables/Data Types
C++ has the basic types that most programming languages have.
int means integer
bool is boolean
char is a single character
float is a floating point decimal number, or a number with a decimal
double is a double precision floating-point number
long int is a long integer
short int is a short integer
void means nothing
declaring a variable is simple, and can be done at anywere in the script, as long as it is before or when it is used.
First say the type of the variable, then say the name of the variable, and finish the line off with a semicolon.
example:
Notice there is no "string" type. You only have char , which is one character. A string can either be made from an array of chars(explained later) or including string.h, which gives you the string variable. Using the string variable is easier for beginners, but less useful for intermediates, you will see why later. Strings are always in "". There are ways to convert strings to char arrays and vise verse. They are both used for different things.
Constants
Constants are almost exactly like variables. To declare a constant, first say const then the type (int, float, char, etc) then the name of the const and finally an equal sign and what it equals. The name must be all caps as it tells the compiler that it is a constant.
example:
C++ is almost entirely based on includes. To include something, just use this format:
Inputting and Outputting
Inputting and outputting are a huge part of C++. In order to input and output, you need to use the include iostream or in out stream. This enables you to use cout and cin. You are probably thinking, what the bleep do those words mean?? Well think of them like this: cout as c out and cin as c in. Get it now? As you may have guessed cout is used when you want the program to output something onto the screen, and cin is used when you want to declare the name of the already declared variable that, whatever inputed from the keyboard, will be stored in.
Whenever using Cout, you must follow it with the << operator, think of these symbols as pointing out. After using <<, you can output a text as a string in ""s or the value of a variable, which will not have "" around it. What if you want to first output "My age is" and then output a variable or const that equals your age (we will call it iAge, the i is for int, just a helpful reminder). then you would do:
For those who think they get it, the << operator is actually saying, cout until a null ( \0 ) character is found. By putting something in ""'s, it is saying it is a string. It automaticly adds a \0 character to the end. If we put it in ' 's, then we would be saying its a character, and in order for it to be a string, it must have \0 at the end. Basicly, null says its the end of the string, and that what was before it was a string.
Take a guess what we use in place of << in cin. If you guessed >>, you are correct. Think of these as pointing into the computer. They are used the same way, only you are saying which variable the users input will be stored in.
The >> operator, reads the user's input, but only the first word, as it stops at the first space. If you wanted to get a whole line, or do other things, you could actually use other operators, such as cin.getline, and cin.ignore (part of the istream class). The << operator also has other operators, which are part of the ostream class. Cin is a istream class (it is a input thing) and cout is a ostream class. I will explain more when i get to classes (classes are advanced, and i don't want to confuse you yet ;))
in the next section, you will learn how to say how you want the info to be displayed on the screen.
Namespaces
When inputting and outputting and pretty much everything else, you need to specify how the info will be displayed. In this tut, we will always be using the standard namespace, std, which is pretty much a black, input output box. If we are using the box for the whole program, we say:
Functions
Functions are declared by first saying the data type that will be returned, then the name of the function. After this, the user inputs when using the function are put in (). If no inputs when the function is used, then still put the (), just with nothing in them. DO NOT FOLLOW THIS WITH A SEMICOLON!
If you want the function to not return anything, then you use void.
example:
void lala()
{ and } are used to state the beginning or ending of a function or loop. The { should be put on the next line after the loop or function is declared. Then at the end, put a }. When you put the }, MS Visual C++ will make the } and the {, that goes with it, bold while you are on it to show you which one it goes with. (excuse my English lol)
Returning
Functions all have to return something. The thing it returns must be the data type you typed before the name of the function. The return can be based of of what the function has done, or it can not.
There are no "procedures", where nothing is returned, in c++, only functions. This is not a problem. The main loop is normally a "procedure" and shouldn't need to return anything, so what most people do is:
The Hello World Program…Taken Apart
I know what your thinking,"Oh god not again." The truth is, the Hello World program is a really good program to start with. It is more complicated in C++ than in Scar, but like I said, far more advanced things can be done with C++.
Here is the Hello World code, do NOT get scared, I will explain everything.
Copy and paste this into a source:
int main(); is declaring a function/procedure
int is the output data type, main is the name, and () is the input. Not that difficult.
look at this line and try to guess what it does:
Once the program finishes, it will close the box in which everything is outputted and inputted. We want the user to see the text, so
System("PAUSE") does the press any key to continue thing.
System is a function, and "PAUSE" is one of the inputs. All system inputs are in all caps. The "" is because it is a string. You get the point.
Finally, we say return 0 because we have nothing else to return, it is a procedure.
In the main loop, it is better to have it be an int, rather than void. I don't know why, it just is.
Starting to become an Intermediate
Get ready to actually start getting into C++.
If Statements
If statements are a big part of programming.
They are declared easily:
Some Useful Operators
== compares two integers, strings, etc. to see if they are equal (note: This will not work with a char array)
>= compares two integers, strings, etc. so see if they are greater than or equal to
<= compares to see if less than or equal to.
> compares to see if greater than
< compares to see if less than
!= compares to see if not equal
! "not" can be put in front of <, <=, etc. and will be true if not that. ! can also be used as "not" for pretty much anything else. (!a==3 is the same as a!=3)
= declares something to be equal to___
"Or" and "And"
Take the statements, q and p. They can be blala==whatever and something else too, but right now, p and q are booleans. Just like if we were to say 1==2 and it would be false, or 1==1 and it would be true, all statements are bools. Now view the table below.

Note how the statements with || and && change from true to false, based on what p and q are.
to be continued when i have more time…
More will be added later! I hope this was helpful. Please post your comments, I worked sort of hard on this.
Please REPLY
Just Finished Massive Update, Enjoy!
Introduction
C++ is a highly powerful programming language used in many professions. There are many rumors that it is difficult. In my opinion, that is bs, though C++ looks scary at first, doing more advance things become much simpler than many other languages. Once you understand it, it will become extremely easy.
I may add a section on how to download a C++ compiler, but right now it is late and I have to go to bed :(
In this tut I will be using Microsoft Visual C++ 2005 as my compiler.
You can download it here
Creating a New Project
First, open Microsoft Visual C++ and go to File>New>Project
In the project types half (left), click Win32 in Visual C++
Then Click, in the right half, Win32 Console Application.
Name the project and click Ok
A window should apear called Win32 Application Wizard. Click next and click empty project in "Additional Options"
Also make sure that Console Application is checked, in the Application type
Now click finish.
Now we need to make a source to type our code in, in the project menu, click add new item. Choose code in the catigories half(left) and C++ file on the right. Name it and click "add"
Now you have a place to type your code.
Variables/Data Types
C++ has the basic types that most programming languages have.
int means integer
bool is boolean
char is a single character
float is a floating point decimal number, or a number with a decimal
double is a double precision floating-point number
long int is a long integer
short int is a short integer
void means nothing
declaring a variable is simple, and can be done at anywere in the script, as long as it is before or when it is used.
First say the type of the variable, then say the name of the variable, and finish the line off with a semicolon.
example:
int x;Almost all lines are finished with semicolons to tell the compiler that the line is finished. The exceptions are includes, if / else lines, while loop lines, and for loop lines. a semicolon is also not needed after { and }, struct and class declaration lines. There are some other exceptions that will be explained later.Notice there is no "string" type. You only have char , which is one character. A string can either be made from an array of chars(explained later) or including string.h, which gives you the string variable. Using the string variable is easier for beginners, but less useful for intermediates, you will see why later. Strings are always in "". There are ways to convert strings to char arrays and vise verse. They are both used for different things.
Constants
Constants are almost exactly like variables. To declare a constant, first say const then the type (int, float, char, etc) then the name of the const and finally an equal sign and what it equals. The name must be all caps as it tells the compiler that it is a constant.
example:
const int MYCONSTANT=2; // MYCONSTANT is now a constant that is an int that equals 2IncludesC++ is almost entirely based on includes. To include something, just use this format:
#include <Whatever> // whatever represents the include you want to includesome includes we will be using in this tut are#include <iostream> //in out stream, needed for inputting and outputting
#include <string.h> //needed to use the string type
#include <stdlib.h> //standard library, needed for many thingsThere are an incredible amount of includes. Google is your best friend for finding these includes, just search what you need something to do in C++ and you should get an include for it. Some includes are in our compiler already, such string.h, stdlib.h, and iostream. Others will be like srl and you will need to install them.Inputting and Outputting
Inputting and outputting are a huge part of C++. In order to input and output, you need to use the include iostream or in out stream. This enables you to use cout and cin. You are probably thinking, what the bleep do those words mean?? Well think of them like this: cout as c out and cin as c in. Get it now? As you may have guessed cout is used when you want the program to output something onto the screen, and cin is used when you want to declare the name of the already declared variable that, whatever inputed from the keyboard, will be stored in.
Whenever using Cout, you must follow it with the << operator, think of these symbols as pointing out. After using <<, you can output a text as a string in ""s or the value of a variable, which will not have "" around it. What if you want to first output "My age is" and then output a variable or const that equals your age (we will call it iAge, the i is for int, just a helpful reminder). then you would do:
cout<<"My age is"<<iAge;when what you are couting changes from a string to the value of a variable or const, and vise versa, you separate them with a <<. If you wish for it to end a line and continue on the next one, use endl (end line) or \n (new line). Remember to separate it with <<'s. Still confused about <<, think about + in scar…sorta. Basicly, << is used to separate different parts of the cout.For those who think they get it, the << operator is actually saying, cout until a null ( \0 ) character is found. By putting something in ""'s, it is saying it is a string. It automaticly adds a \0 character to the end. If we put it in ' 's, then we would be saying its a character, and in order for it to be a string, it must have \0 at the end. Basicly, null says its the end of the string, and that what was before it was a string.
Take a guess what we use in place of << in cin. If you guessed >>, you are correct. Think of these as pointing into the computer. They are used the same way, only you are saying which variable the users input will be stored in.
The >> operator, reads the user's input, but only the first word, as it stops at the first space. If you wanted to get a whole line, or do other things, you could actually use other operators, such as cin.getline, and cin.ignore (part of the istream class). The << operator also has other operators, which are part of the ostream class. Cin is a istream class (it is a input thing) and cout is a ostream class. I will explain more when i get to classes (classes are advanced, and i don't want to confuse you yet ;))
in the next section, you will learn how to say how you want the info to be displayed on the screen.
Namespaces
When inputting and outputting and pretty much everything else, you need to specify how the info will be displayed. In this tut, we will always be using the standard namespace, std, which is pretty much a black, input output box. If we are using the box for the whole program, we say:
using namespace std; if we didn't say this, we would have to type std:: before cout, etc. everytime we want to output something. This can be useful if we want to use different namespaces for different things. Declaring a constant namespace always comes right after the includes.Functions
Functions are declared by first saying the data type that will be returned, then the name of the function. After this, the user inputs when using the function are put in (). If no inputs when the function is used, then still put the (), just with nothing in them. DO NOT FOLLOW THIS WITH A SEMICOLON!
If you want the function to not return anything, then you use void.
example:
void lala()
{ and } are used to state the beginning or ending of a function or loop. The { should be put on the next line after the loop or function is declared. Then at the end, put a }. When you put the }, MS Visual C++ will make the } and the {, that goes with it, bold while you are on it to show you which one it goes with. (excuse my English lol)
Returning
Functions all have to return something. The thing it returns must be the data type you typed before the name of the function. The return can be based of of what the function has done, or it can not.
There are no "procedures", where nothing is returned, in c++, only functions. This is not a problem. The main loop is normally a "procedure" and shouldn't need to return anything, so what most people do is:
int main()
{
//bla bla, put ur code here
return 0
}We just returned something, and we don't care what it is because we are not going to use the return.The Hello World Program…Taken Apart
I know what your thinking,"Oh god not again." The truth is, the Hello World program is a really good program to start with. It is more complicated in C++ than in Scar, but like I said, far more advanced things can be done with C++.
Here is the Hello World code, do NOT get scared, I will explain everything.
Copy and paste this into a source:
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World! This is my first program in C++"<<endl;
system("PAUSE");
return 0;
}
First I included iostream, or input/output stream for your int main(); is declaring a function/procedure
int is the output data type, main is the name, and () is the input. Not that difficult.
look at this line and try to guess what it does:
cout<<"Hello World! This is my first program in C++"<<endl;This line is outputting the text in the "" and then, using endl, ending the line.Once the program finishes, it will close the box in which everything is outputted and inputted. We want the user to see the text, so
System("PAUSE") does the press any key to continue thing.
System is a function, and "PAUSE" is one of the inputs. All system inputs are in all caps. The "" is because it is a string. You get the point.
Finally, we say return 0 because we have nothing else to return, it is a procedure.
In the main loop, it is better to have it be an int, rather than void. I don't know why, it just is.
Starting to become an Intermediate
Get ready to actually start getting into C++.
If Statements
If statements are a big part of programming.
They are declared easily:
if(a==0){
//bla bla, what to do if the above is true
}
else{
//bla bla
}Notice how I put == instead of =. This is because I am comparing 'a' and 0. If I had put a=0, then a would now equal 0. Before we move on, lets get familiar with some of these operators.Some Useful Operators
== compares two integers, strings, etc. to see if they are equal (note: This will not work with a char array)
>= compares two integers, strings, etc. so see if they are greater than or equal to
<= compares to see if less than or equal to.
> compares to see if greater than
< compares to see if less than
!= compares to see if not equal
! "not" can be put in front of <, <=, etc. and will be true if not that. ! can also be used as "not" for pretty much anything else. (!a==3 is the same as a!=3)
= declares something to be equal to___
"Or" and "And"
Take the statements, q and p. They can be blala==whatever and something else too, but right now, p and q are booleans. Just like if we were to say 1==2 and it would be false, or 1==1 and it would be true, all statements are bools. Now view the table below.

Note how the statements with || and && change from true to false, based on what p and q are.
to be continued when i have more time…
More will be added later! I hope this was helpful. Please post your comments, I worked sort of hard on this.