Well, I'm new here. So I don't know how much this site is into programming. But here's a lil C++ tutorial I wrote.

C++ Tutorial

I. Introduction
II. Syntax
III. Statements
IV. Functions
V. Headers
VI. Operators
VII. Data Types

This tutorial is meant as a guide to learning C++. It will not explain the finer points of the language. It assumes you are familiar with at least one other programming language. If you are not, I strongly suggest you learn one before continuing with this tutorial, as it focuses on the just the syntax and functions of C++. At the end of each section are examples of everything from the section in the order that they appeared.

The syntax for C++ is quite similar to that of Perl.
1. Every declaration of a variable must end with a semi-colon (.
2. Every statement must end with a semi-colon(.
3. All compound statements must begin with an opening brace ({) and end with a closing brace (}).
4. Whitespace can be used wherever needed.
5. All expressions are statements and therefore must end with a semi-colon.
6. The body of a function must be enclosed by opening and closing braces.
7. Comments in C++ can either be C++-style (//) which comment the rest of a line; or they can be C-style (/* */) which comment whatever is between the stars.
8. Declarations of variables must assign a variable a data type ex. int var
9. Variable names must begin with a letter and may only contain letters, numbers, or underscores.
10. When writing a function you must define a data type, the name of the function, followed by an open and closed parentheses (), then an opening brace ({), the body of the function, and ended with a closing brace (}).
11. When including a file, the filename must be surrounded with either quotes (" ") or enclosed in greater than and less than signs (< > ).
12. All functions must return something. main() must return 0 .
13. To input text, use cin. The syntax for cin is cin >> variable_where_input_is_stored;
14. To print text to the screen, use cout. The syntax for cout is cout << "Whatever text you want"; (to create a newline, use \n.)

if...then - tests for a condition to be true; if it is, the body of the statement is executed
if...else - tests if a condition is true; if it is it executes the body of the if statement, if it is false then it executes the body of the else statement.
switch - basically it is a multiple "if" statement

if (a == 4)   // statement; tests if a is equal to 4 
    cout << "a is equal to 4\n";    // body; if statement is true then the body is executed 
if (a >= b) 
    cout << "a is greater than or equal to b"; 
    cout << "a is not greater than or equal to b"; 
case 1:
 cout << 'my_var is set to 1';
 break; /* without using break the statement will "fall through"  this means that all other statements below it that are true will  also be executied */

case a:
 cout << 'my_var is set to the letter a';
default:   //default acts as "else" in an "if-else" statement
while - the loop is ran as long as the condition in parentheses is true
do...while - makes sure the body of the statement is executed at least once before the condition is tested
for - sets up starting condition, tests it, and changes variable all in one loop; must be in that order

while (a <= c) 
    cout << number "\n"; 
while (number < 10) 
for (number = 3; number < 10; ++number) 
    cout << number"\n";

A function is a subprogram that can act on data and return a value. Every C++ program contains at least one function. That is int main() . You can define a function for whatever your needs may be. To define a function, you must specify the data type for it to return, the name of the function followed by () to contain any parameters (we'll talk more about those later), and the body of the function. The body says what the function is going to do and is enclosed with an opening brace ({) and a closing brace (}). A variable and function may share the same name, however this can be confusing so I don't recommend it. C++ has many functions already defined. They are included in header files (we'll talk about those in the next section). To see what functions have been already defined just browse through the header files you have included. To use them just type them in with the proper parameters (pretty complicated right?). You can program a function to do whatever you wantl; it can contain variables (if defined inside the function then they are considered local variables and do not exist outside of the function), loops, statements, compound statements, and anything else you could want to put in it.

Now, about parameters, they are variables that can be passed into the function. I've tried to clear this up in the second example below, so if you don't understand me, check that out.

int main () 
body goes here 
int addition(int a, int b) // declaration of function 
    return (a+b); // body of function; returns the sum of a and b 
cout << "The two numbers to be added are " << a << " and " << b << " .\n"; // prints the numbers 
cout << "The sum is " << addition(3,4) ; /* passes the parameters 3 and 4 into the function and        prints the result */ 

Header Files
Header files are very important in C++. The most important is probably iostream.h . This header allows you to input and output data. Meaning it lets you use cin (inputs data), and cout (outputs data; writes data). This header will be included in just about every single program you write. To include a header you must call to the preprocessor first, then tell it to include the file, then tell it what file to include. This is shown in the first example below.

You usually only want to include header files that you have written for classes (will be discussed later) or objects (discussed w/ classes). Otherwise try to minimize their use. Unless they are needed for using a particular function.


#include <iostream> /* the pound sign (#) calls the preprocessor, include tells the preprocessor that what comes next will be a filename and to put it in as if it had been typed there; and <iostream> is the file */

Basic Operators
+ Addition operator
- Subtraction operator
* Multiplication Operator
/ Division Operator
% Modulus Operator (takes the remainder of a division problem i.e. 20 % 6 = 2)
= Assignment Operator (i.e. a = 3; Takes 3 and assigns it to a)
^ Exponent operator

Conditional Operators (test for truth/falseness)
== Equality operator
!= Non equal operator
<= Less than or equal to operator
>= Greater than or equal to operator
Logical Operators
|| or
! not
Incremental Operators
++ Auto increment operator
-- Auto decrement operator

Data Types

type                    size            	range 
unsigned short int      2 bytes                 0 - 65.535 
short int               2 bytes                 -32,768 - 32,767 
unsigned long int       4 bytes                 0 - 4,294,967,295 
long int                   4 bytes                 -2,147,483,648 - 2,147,483,647 
int                     2 bytes                 -32,768 - 32,767 
unsigned int            2 bytes                 0 - 65,535 
char                    1 byte          	256 character values 
wchar_t                 2 bytes                 65.535 character values 
bool                    1 byte          	True or False 
float                   4 bytes                 1.2 x 10-38 - 3.4 x 1038 
double                  8 bytes                 2.2 x 10-308 - 1.8 x 10308 
long double             10 bytes        	3.4 x 10-4932 - 1.1 x 104932

Well that is my tutorial on C++. I hope you found it useful. Check back soon, as it will constantly be under revision. I will also try add some more advanced aspects in future revisions. Check back to the C++ section soon for more tutorials on Polymorphism, Socket Programming, and Advanced Functions.
If you would like to contact me, please email me at hcir614@hotmail.com