Okay here are two chapters I wrote for my site a while ago. I thought I'd post them here. There might be some technical errors, so sorry.

C++ Programming (Chapter One) by Jethro

Topic: Programming

http://jethrojones.hyperlinx.cz


-------------------------------



This tutorial is about basic programming with C++. Very basic.

If you are a newbie however, you many find this tutorial quite useful...

We cover basics like variables, functions, compilers,

headers, operators...etc



C++ (pronounced "see-plus-plus") is an extended version of the

C language which was developed in Bell Labs in 1978. In the

early 80's, C++ was created as an object-orientated language.

Without going into too much detail on the history of C++, I'll

tell you this: C++ is the language which most software developers favour,

because of its power, ease-of-use and its acceptance

on most systems, such as Windows and *NIX.



To make a C++ program you will need a compiler. I use Dev-C++ for

Windows (http://www.bloodshed.net) but there are good ones such as

Borland C++ Builder 5.5 (http://www.borland.com) and for UNIX, the

famous GCC compiler is the best. This comes with a lot of UNIX systems

like Red Hat Linux, I believe.



Before the code can be executed, it must be linked to other

pieces of code (e.g. included libraries) used by the program. The

compiled & linked program is called an executable file. Finally,

the program is executed by the system. The compiler does this.

Compilers takes your typed source

code and translates it into machine code (1s and 0s).



Here is an example program:



************ helloworld.cpp ************



Code:
 /* This is a comment

    Hello World program

    Written by Jethro */

 

 #include <iostream.h>

 

 int main()

 {

    cout << "Hello World" << endl;    // First statement

    return 0;                         // Second statement

 }

*****************************************



Okay, time to go through this line-by-line...





*****************************

/* This is a comment

Hello World program

Written by Jethro */ = This is a comment. There are two types

of comments. Comments which run over a few lines (multiline) or

single-line comments. This is an example of a multi-line comment.

Everything between "/*" and "*/" gets ignored by the program itself.

Comments are good for people who read your code, so they know what

everything does so they can fix/debug it easily.

*****************************

#include <iostream.h> = This tells the compiler that the "iostream.h"

library file is needed and should be included when compiling the code. A

library is a collection of program code that can be included (and used) in

a program to perform a variety of tasks. "iostream.h" is a library

(or "header") that performs certain input and output functions. You can get

these library files (if for some strange reason they didn't come with your

compiler) at loads of different websites like http://code.box.sk.

*****************************

int main() = Okay, this is fairly complicated but bare with

me. "int main()" is a function header. "main()" is the function name. A

function is a collection of statements. A function header includes the return

type of the function (what it returns to the place it was called when it exits)

and the function name, in this case "main". This function returns an INTeger

through the line "return 0", but more on integers and the RETURN statement later.

So all the functions that have an integer as the return type return integers.

All the statements in a function are enclosed in curly brackets "{" and "}".

This means that everything between the open bracket "{" and the closed bracket

"}" are part of that function. I hope I didn't make that sound too complicated.

You'll get it eventually, even if you don't understand it now.

Note: For a function to be executed it needs to be called. You can call a

function from anywhere, just by entering its name, like "askName()", except

for the main() function. This is called automatically when the program is

executed. This is why we don't call it. main() funcions usually return an integer.

*****************************

cout << "Hello World" << endl; = This prints "Hello World" to the screen.

"Hello World" is a string of text (in programming, there's no other kind of

string, other than a text string).

This line is an example of a stream. A stream is an input/output

device. "cout" (pronounced "see-out") is the Console OUTput stream. "iostream.h"

needs to be INCLUDEd when we use streams. First we send "Hello World". We send it

using an operator. The "<<" operator. This basically means, send the string to the

cout stream. Then we also send "endl" (this is optional). endl means END Line, so

if we send some

sort text to cout, it will appear on a new line. All statements are ended with a

semi-colon ";".

****************************

// The first statement

// The second statement = These are both examples of one-line comments.

****************************

return 0; = This returns the integer 0 from where it was called. In the

case of the main() function, this just means it exits fine. The main() function

should always return a 0. More of the return command later.

*****************************



And that is helloworld.cpp explained. Just compile that and view it.





Variables.



Variables are pieces of information stored on the memory. There are three parts

to a variable. Its name, type and value. Its name is just what we call it. Its

value is the information it holds. This can be an integer, a decimal, a string...etc.

It's type is defined in accordance to the information it is going to hold:



int = -32,768 to 32,767

long int = -2,147,483,648 to 2,147,483,647

float = 1.2 x 10^-38 to 3.4 x 10^38

double = 2.2 x 10^-308 to 1.8 x 10^308

char = 256 character values



Here is an example program which shows this:



************** numbers.cpp ***************

Code:
/*  To show variables in action

    Numbers program

    Written by Jethro         */

#include <iostream.h>

#include <stdlib.h>

 

int main ()

{

  int num1;

  int num2;

  cout << "To show variables" << endl;

  cout << "Please enter a number: ";

  cin >> num1;                                   // User input #1

  cout << "\nPlease enter another number: ";

  cin >> num2;                                   // User input #2

  cout << num1 << " + " << num2 << " = " << num1+num2 << endl;    // Add

  cout << num1 << " - " << num2 << " = " << num1-num2 << endl;    // Subtract

  system("PAUSE");

  return 0;

}
******************************************



New stuff:

*********************************

#include <stdlib.h> = This is another example of using one of the

countless header files available in C++. We need to include "stdlib.h"

because we use the SYSTEM() function to interface with MS-DOS.

*********************************

int num1; and int num2; = Here we declare two integer-type variables.

We call them (originally enough "num1" and "num2". But you can call them

"drag" and "weed" if you want, it doesn't matter. Just remember that C++ is

CaSe SeNSiTiVe, so num1 is not the same as Num1, nUm1 or even nuM1! We don't

give them values, we can assign them values later... Anyway, as long as you

know that that means they are integers, it's okay.

********************************

cin >> num1; = "cin" (pronounced "see-in") is another example of a stream,

courtesy of "iostream.h" again. This means that everyhing the user types, up

to the carriage return (aka. "Enter"), will be included into the variable "num1".

Notice the way we use ">>" to symbolise that the information is going from

"cin" to "num1", not vica versa, as is in the "cout" stream. We did not use

"endl" on the previous line, because we wanted the user to type the number

right after "Please enter a number: ".

*******************************

cout << "\nPlease enter another number: "; = Your probably wondering why

I put '\n' there. Well, it's simple. \n means go to a new line. Similar to

endl. The following line:

'cout << "\n\n\nThree lines down";' would print out the words "Three lines

down", three new lines down.

*******************************

"num1+num2" and "num1-num2" = This is the use of operators. num1+num2

returns the value of num1 added to num2 and num1-num2 gives the value of num2

subtracted from num1. This does nothing to the actual value of the variable

however.

+ = Addition

- = Subtraction

* = Multiplication

/ = Division

*******************************

system("PAUSE") = The system() function, courtesy of "stdlib.h",

lets you run an MS-DOS command. If you have read any of tutorials (I have

written a few) on MS-DOS, you will know that this creates the prompt "Press

any key to continue..." and will not continue executing the program until

the user has pressed a key.

*******************************



And that's numbers.cpp...



I think that's enough for Chapter One. In Chapter Two I will talk about

functions, conditional statements, loops and whatever else I think of.



If Chapter Two hasn't been written yet, check back in a few days.



**************** EOF ***************