C++

I found that I always learned better by quick instructions and hands on work with examples. So..that's how this tutorial will go. Short and sweet.

I think I should lay some ground rules before we begin. First off..you MUST experiment. 80% of my computer knowledge comes from experimentation. So you must.
Also, you can email me with questions, but before you do look around a bit. Try other stuff. I think I'm pretty good with C++, but I don't know everything. I still get those programs that have 100 sum ought errors...hey, it happens..

That said, let's begin.


// File: prg1.cpp

#include <iostream.h>		// controls the input and output

void main()			// call main()
{
	int a, b;		// declare 2 variables to hold intergers
	a=1;			// sets a to equal 1
	b=1;			// sets b to equal 1

	cout << a+b << endl;	//outputs a+b to screen
}

OUTPUT:
THis program outputs a single digit. If you compiled and ran this program you would see that 2 is printed on the screen, then the program suddenly ends.

ANALYSIS:
In line 1 we use #include . It will tell the compiler to take the contents of iostream.h(a header file) and include them during the compilation. The <>s tell the compiler that include.h is in the compilers include folder. If you use quotes around iostream.h it tells the compiler to look in the same directory as prg1.cpp. I will explain when and how to use that later on in the tutorial.
main() is a special function that is called, or executed, everytime your program starts. This means that everything in the {}s will be executed. You will also learn how to make your own functions later in the tutorial.
Then we get into "variables". Variables are a way to store data while your program is running. int is a specific "data type". Data types can only hold what they are set to hold. int is one of 4 basic data types. The other basic data types are: double, char, and bool. Int hold an interger, a whole number with no decimals. double, holds a real number, this means that double can hold decimals. A char hold a single character(one of any symbol, letter, or number). And last but not least, bool holds a boolean value..true or false. bool can either be asigned as true or false or given a number value, everything but 0 being true. It is vital to use the right data type. You can't add chars, easily. And you can't divide bool. So whenever you're writing a program, you must know what kind of data your variable will be holding.
Moving on, the line 6 declare 2 int variables for usage. Lines 7 and 8 tell the computer to assign those variables the value of 1 and 1.
Line 9 does everything that you can see. cout is a form of output. It tells the computer to print the result of a+b or 1+1 which is of course...2. You may notice that there are << between this and cout. cout is an output stream. To use streams, you say which stream, and then use the << "opperator" to use the stream. endl tells the computer to skip down a line in the terminal.

Simple enough? I hope you said yes, but if you didn't, play around with the code. Experimentation is a big part of learning to program. Change the variables, change the types, just mess with it. And if you're having problems, my email box is always open.


Download: prg1.zip

Tell me what you think.
Hosted by www.Geocities.ws

1