Tutorials : Basic C++ School
Date: Sunday, February 02 2003
Topic: Programming


This will be the first lesson in our new C++ school. You'll learn the basics of programming. And hopefully you will be able to do simpel animations in the end.

The begining

Many people always ask me lot of simpel C++ questions. It can be all from syntax to whole code. So I wrote this tutorial so answer all those questions in one small C++ school.

My code is optimized for the Borland c++ compiler (Borland C++ Builder 5.02) so some functions may not work in other compilers like MS Visual C++. If you can't effort buying those you can always download Dev-C++. It's free and you can download it at http://sourceforge.net/projects/dev-cpp . Just remember that most of my examples wont work in other compiler than Borland's. I'll tell when they don't.

But we will start with an easy example only so you get used to C++.

#include <iostream.h>

int main()
{
   return 0;
}

this code is everything you need to get a program work. Just copy this to your compiler and you'll see it works ;)

Then you may ask what everything means. Let start with line one. First you need to show the program which library it will find the functions. In this case we are not using any functions so the first line isn't needed. But we will use it anyway to get used to it. Then the fallowing line int main() is the main function. I'll tell you later what functions is. Every C++ program need to have a main function. So the minimum code to write and still get a program runing would be main() {}. When you go to the third line you'll notice the { and later on the }. All code comes between those brackets. You'll notice that other functions will use them too so just get used to them. The last line return 0; means that that functions does't return nothing. Just get used to that now and I'll explain it all later

Comments and variables

if you wanna write a comment to your text which is smart... then do it like this.

for one line comments:
// this is a comment <= starts with //

if you gonna have a comment on few lines then use:

/* this is a comment <= everything between /* and */ will be a comment
this too
and aswell as this
*/

then there are some shortenings in c++:

int
double
float
char
bool

those are the most common

int stands for integer and holds numbers like 1 2 3 4 5 6 7 8 9 and so on
double is for decimal numbers like 1.3 6.2 6.8 1256.21352
float is like double but for smaller numbers... double can have numbers up to 2 billions or something...
char is holding characters... for example " % & ! f l i h n b
bool is a variable which holds a true or a false... a 1 or a 0... a yes or a no


to make variables, write. Variables are temporary places to store data.

int variable; // notice that all sentences ends with a ;
/*
this line means that you are creating a variable named "variable" and telling it to only use integers.. if you give it the value 6.85 it will only use 6! <= notice that!!
/*

but the variable is empty... to give it a value your write:
int variable = 10;
it's easy!

in c++ you can use normal math like + - / * .. so
10 * 10 works as well as 65 + 23

I can write:
int variable = 10 + 62;
the puter calculates 10 + 62 and gives the answer to the variable.

try to find out what this code do.


#include <iostream.h>

main()
{
   int a = 5;
   int b = 6;
   int a = a + 1;
   int c = a + b;
}

Print to the screen

Now you know how to save calculations in variables and so on so lets print stuff to the screen write:

cout << "This will appear on the screen";

to skip line write:

<< endl;

so if I would write my name and skip a line it would be:

cout << "kajo" << endl;

if I would skip a line..write my name and skip 2 lines:

cout << endl << "kajo" << endl << endl;

to output a variable write:

cout << "The variable is " << variablename << endl;

you can also make calculations in the cout.... you need to put it in paranthesses.. ()

example:

cout << "Answer: " << (a+b) << endl;

you got it I hope smile.gif


to input things to the screen you write:

cin >> variablename;

example:

main()
{
    int users_input;
    cout << "Input a number: ";
    cin >> users_input;
    int answer = users_input * 10;  // get the users_input times 10
    cout << var << " times 10 will be " << answer << endl;
}

 

Not so hard. Hope you'll understand it all. If you don't you're free to discuss and ask questions at the forum. Next lesson will talk about how to make your program do different things depending on different stuff.. called if statements :)

 

good luck with this!

 





This article comes from osforge.com
http://www.osforge.com

The URL for this story is:
http://www.osforge.com/news/00934.html