Jump to content

Tutorial-the basics of C++ and game making


Recommended Posts

You can make variables accessible for all functions includeing the main program ("void main()": it's a function) by making them global.

When you declare a variable inside a function, it's accessible only inside that function. However, if you declare a variable outside of functions, it's accessible to the entire program (all the program after the decleration, of course):

#include<iostream.h>
int num=0;
void func()
{
 num++;
}
void main()
{
 num++;
 func();
}

 

"const" variables and "enum"s are allso following that rule. "#define"s, however, is accessible to the entire program after the command, since it's precompiling action.

 

That will be all for today. Class dismissed.

Link to comment
Share on other sites

  • Replies 68
  • Created
  • Last Reply

Top Posters In This Topic

Sometimes you don't want to know what number you will use. You want to generate a random number. There are some functions that generates a random number. To use those functions, you need to include "stdlib.h":

#include<iostream.h>
#include<stdlib.h>
void main()
{
 randomize();
 cout<<random(10);
}

the output will be any number between 0 and 10, includeing 0 not includeing 10.

We can think about those functions like a lottory. Randomize mix up the balls, and random pick a ball. Use randomize only once, before the first random, or you will get the same number any time you use random. If you don't use randomize, you will get the same numbers every time you run the program.

 

That will be all for today. Class dismissed.

Link to comment
Share on other sites

Bit off topic, but does anyone know that the topic title is spelt wrong. Maybe someone in charge could change it.

 

Not that important I know, but someone may as well change it.

Link to comment
Share on other sites

This subject is complicated, so don't be too shy to ask questions.

 

Pointers are very powerfull tools in programing. Wo what are pointers?

Every variable has an address. The address tells the computer where in the memory the variable is located. A pointer is a variable that can store such addresses.

 

To declare a pointer, add '*' in before it's name:

int *p;

p is a pointer to integer.

 

If you add '&' before a variable name, you get the variables address. You can put that address inside a pointer:

int a,*p;
p=&a;

 

To use the value in the address the pointer holds, put '*' before the pointers name:

int a=0,*p;
p=&a;
*p++;

a is now 1.

 

You can create variable on the fly using pointers like this:

int *p;
p=new int;

"new int" find an empty place in the memory, and return it's address. You can to it to other types of variables:

char *c;
float *f;
c=new char;
f=new float;

 

You can release the memory like this:

int *p;
p=new int;
delete p;

 

That will be all for today. Class dismissed.

Link to comment
Share on other sites

In the memory, the cells of an array are placed by order. The array variable is basicly just a pointer to the first cell of the array. Thats the reason you can send an array to a function and change it inside the function. Thats allso the reason you can't copy one array to each other like a regular variable.

 

You can create and delete an array on the fly like that:

int *a;
a=new int[5];
a[2]=2;
delete [] a;

 

Notice that you don't write "*a[2]=2", because the [] already tell the compiler you refer to a pointer.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...