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

Lesson 6


Now from a hard lesson, I thought you might be tired, so if you understand the last lesson, this one should be easy.

Functions and pointers
When passing variables down to a function, you are just sending its value (a copy) of the variable, so no matter what you do, you can not change the variable you sent down to the function within the function.
Example

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

void Change(int &a);

void main()
{
	int a=5;
	cout << "The value of a is: " << a << endl;
	Change(a);
	cout << "The value of a is: " << a;
	getch();
}

void Change(int a)
{
	a=25;
	return;
}

Output
Number is: 5
Number is: 5

Note how the variable Number did not get changed by the function. As this is pass by value. But now if we wanted to change the variable Number within the function My_function(int a); we simply use pointers. How you ask, well we send the memory address (location) down to the function instead of its value, then we point to that address and change what ever is in that memory location.
Example

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

void Change(int &Number);

void main()
{
	int Number=5;
	cout << "The value of Number is: " << Number << endl;
	Change(Number);
	cout << "The value of Number is: " << Number;
	getch();
}

void Change(int &Number)
{
	int *pointer;
	pointer=&Number;			//Point to the memory location held by a
	*pointer=63;			//Change the value in that memory location
}

Output
Number is: 5
Number is: 63

Now we can change the variables send down to the function. I simply sent down the variable Number - Number, then I told it i wanted its address - &Number. Then I pointed to the address of Number. Then I changed the contents of that memory location - that memory location - *Pointer = 63; and output Number again in the main function. This technique is used alot in programming, it is very usefull if say you wanted to have a function change many variables, rather than just 1, which is all a function can do if you dont use the above technique, Just send many variable addresses down to the function, point to them, and change them.

Stacks
Right, stacks can be thought of as literally a stack, when you stack books up on top of each other, that is a stack. With stacks, the first one you put in, is the last to come out. Eg: If you stack a pile of books on top of each other, the one at the bottom was put in first, but when you want to put them onto another pile, you take the top one of first, then the next .........., and the bottom one comes out last. When you put data in, its called push(data), when taking out, its called Pop(). Stacks also have a stack point, this is always 1 place above the last data put in, Ill call it S.P.
Example
Push(3,78,42,56) Pop() Pop()
S.P  
56S.P 
4242S.P
787878
333


Now ill show you how to create a stack. We use an array and a bit of maths.
Example

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

int Pop();
void Push(int a);

	int top,Stack[20];		//Global, not recomended

void main()
{
	cout << "Push 3,78,42,56 on to the stack." << endl;
	Push(3);
	Push(78);
	Push(42);
	Push(56);
	cout << "Stack size is: " << top << endl;
	cout << "Pop 1st one out: " << Pop() << endl;
	cout << "Pop 2nd one out: " << Pop() << endl;
	cout << "Now the stack size is: " << top;
	getch();
}

int Pop()
{
	if(top>0)
	{
		top--;
		return Stack[top];
	}
	else
	{
		cout << "Stack is empty.";
	}
	return 0;			//Dummy return value.
}

void Push(int a)
{
	if(top<20)
	{
		Stack[top]=a;
		top++;
	}
	else
	{
		cout << "Stack is full.";
	}
}

Output
Push 3,78,42,56 on to the stack.
Stack size is: 4
Pop 1st one out: 56
Pop 2nd one out: 42
Now the stack size is: 2

In this I made the variable top the Stack Point. I also defined the stack as a global array which is not recomended, ill show you how to do it in a class next. I put the numbers 3,78,42,56 on to the stack in that order, then I output the Stack point(size), then Pop'ed 2 out which were the last 2 numbers to go in. Quite a simple program but interesting.

Many big programmers (picky buggers) , say it is bad to use global variables as they might clash with the ones you define in your functions, well it sure can make life difficult, but if they say so then I guess I should show you how to do it another way so now lets try it with a class. Same example but using a class -

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

class My_class
{
private:
	int top,Stack[20];
public:
	void First()
	{
		top=0;
	}
	void Push(int a)
	{
		Stack[top]=a;
		top++;
	}
	int Pop()
	{
		top--;
		return Stack[top];
	}
	int Size()
	{
		return top;
	}
};

void main()
{
	My_class My_stack;			//Create an instance of the class
	My_stack.First();			//Initialize class variables - very important!
	cout << "Push 3,78,42,56 on to the stack." << endl;
	My_stack.Push(3);
	My_stack.Push(78);
	My_stack.Push(42);
	My_stack.Push(56);
	cout << "Stack size is: " << My_stack.Size() << endl;
	cout << "Pop 1st one out: " << My_stack.Pop() << endl;
	cout << "Pop 2nd one out: " << My_stack.Pop() << endl;
	cout << "Now the stack size is: " << My_stack.Size();
	getch();
}
This is very much the same as above, exept now the functions are within the class, and you have to call it by - instance name dot function. I also added the function Size() to get the size of the stack (top). IMPORTANT - you must initialize varaibles, else it causes a crash, the variable top must be initialized to 0! Else when you use top++; top will become a stange number out of the Stack array size. Everything else is identical to the first example.

Note how most of the processing was done within the class itself, well this is where C++ gets its object orientated name from, the instance of the class My_stack was an object, I could do almost all the work within the class itself, I only told the class what to put in, and asked it to Pop a number, the class itself did all the work. Welcome to Object Orientated Programming!

Feeling lucky? Try the next Quiz - Click here


Back Home Next