Free Web Hosting by Netfirms
Web Hosting by Netfirms | Free Domain Names by Netfirms

Lesson 1


C++ is an object orientated language from C, it includes classes which are objects in a program, this is where it gets its object orientated name. Both languages are similar, but C++ includes extra items which gives it the advantage.

Comments
//Bla bla bla
The two // are for comments, any text after them on the same line are for comments and will not be interpreted by the compiler.
/* Bla Bla Bla */
The /* followed by */ is also comments, but will comment everything out until the */ is found


Include files
#include < filename.h > is a must in any program. This tells the compiler where to get the source to find how to do the common functions.
The common ones to use are: conio.h, iostream.h, stdlib.h, stdio.h, math.h, string.h
these are all the files you will need to know for all of these tutorials, but mainly the first 2.
Example:
#include < conio.h >
#include < iostream.h >

....................		//Rest of program
Note: for this tutorial, do not use spaces between the arrows and filename like this: < filename.h >, as i am writting this in html, and is hard to exclude the spaces, sometimes i will but sometimes i wont cause its takes ages, so if you see any include files with spaces, take the spaces out before compiling. it should look like this: <conio.h> and so on for all include files.


First program
//Copy and paste this or type into your compiler and run it


#include <conio.h>
#include <iostream.h>

void main()
{
	cout << "Programming in C++ rulz";
	getch();
}

The void main() means that the function "main()" does not take in any variables or return any.
We will go into functions and passing variables in later chapters, for now its just something to remember as being the the main line of text in the program and is the same everytime. Next is the brackets { and } these are used for functions, loops, and other more advanced things we will learn later. The cout<<"...."; stands for console output stream, the 2 << and double quotes around text tells it we want to out put text, also the semicolin ; is very important, and is used to stop every statement. The getch(); pauses the program until the user presses a key, else the program will run and quit before you even read anything.
If the semicolin is left off the output statement then the statement will go on and on, as shown below
This is another possible way of outputting lots of text to the screen:

#include <conio.h>
#include <iostream.h>

void main()
{
	cout << "Programming in C++ rulz" << endl
	     << "and so does this tutorial" << endl
	     << "cause it just goes on and on";
	getch();
}
The "endl" word tells it to put the text on the next line, the same thing can be achived by putting a: \n within the quotes.


Variables
Variables are the heart of every program. They can be any number, characters, or boolean - true or false.
You must declare every variable before you use it either within the function (Local) or outside (Global).
Example:

#include <conio.h>
#include <iostream.h>

void main()
{

	int number;
	cout << "Please enter a number: ";
	cin >> number;
	cout << "Your number was: " << number;
	getch();
}

Firstly i declared number to be an int (integer), i did this within the function, which makes the variable number a Local variable. Which means only statements within the function main() can change it. The cin>> is short for console input stream, the >> or << stand for stream by the way. This means that it will wait for the user to enter something and then press enter. Finally on the last line, to output variables, put them within the two << and << arrows. In my case, i ended the statement so i did not need the other << on the end.


Variable types
Type Name Definition
int Integer Is a number between about -20000000 and +20000000, but not any decimal values.
float Floating point number A huge number which has decimal values, eg: 24567.4567
char Character A single letter from the keyboard, technically its an (ASCII code).
bool Boolean only has 2 values - true or false same as (0 or 1).

These are all the types we will be using, there are many others, but its not relevant for this section.
Take this Quiz if you want to test your current knowledge - Click here




Back Home Next