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

Lesson 7



Classes - Part 2
Now ill teach you about constructors, destructors and external class functions.
A constructor is automatically called when an instance of a class is created, but the destructor is not. A constructor creates an instance of all the variables in the class, the destructor clears them (deletes them) when the class is no longer in use. A constructor must be named the same as the class name, and looks like a function. The destructor also has the same name but has the symbol ~ before it. eg:

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

class My_class
{
private:
	int Number;
public:
	My_class()		//Constructor
	{
		Number=0;
	}
	~My_class()		//Destructor
	{
	}
	int Value()
	{
		return Number;
	}
};

void main()
{
	My_class Instance1;
	cout << "Number is: " << Instance1.Value();
	getch();
}

Note how I preset the private variables in the constructor, this is so you dont get strange values from it. The destructor is automatically called when the class is not used any more, so whatever you put in there will be executed when the class is not used again, this would be commonly used for freeing up dynamic memory, which we will learn later.

External class functions
So far I have only showed you class functoins which are within the class itself, but you can also put them outside of the class . Like this -
Class_name :: Function() , so you put the name of the class first, followed by 2 colins (not semicolins), then the function. Also you need to make the function a member of the class by writting the function name inside the class.
Example

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

class My_class
{
private:
	int Number;
public:
	My_class()		//Constructor
	{
		Number=0;
	}
	~My_class()		//Destructor
	{
	}
	int Value();		//Make the function Value() a member of My_class
};

My_class::Value()		//Write out external function
{
	return Number;
};				//Use a semicolin to terminate external functions

void main()
{
	My_class Instance1;
	cout << "Number is: " << Instance1.Value();
	getch();
}
The constructor should have preset the variable Number to 0, so the output should be 0.

Dynamic memory
This means memory which can be created / deleted during runtime(When the program is running). It is useful when you do not know how much memory will be needed during the runtime of the program. New memory can be allocated by using a pointer, then using new datatype. So if you want a new integer, you write new int;. To allocate new memory, we must use pointers, like this - int *pointer; pointer=new int; you access the data by using the pointer with a star, just treat it as a usual pointer. Now we must always remember to delete it after we have used it, by using - delete pointer;
Example

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

void main()
{
	int *Pointer;
	Pointer = new int;		//Point to a new 4 bytes (integer) of memory.
	*Pointer = 56;			//Give that 4 bytes of data the value 56.
	cout << "Pointer = " << *Pointer;
	delete Pointer;			//Important to delete it after use.
	getch();
}
First I declared a Pointer which was to point to an int, then I made it point to a new free spot in memory (windows allocates the memory) with 4 bytes free(integer). Then I just treated it like a normal Pointer.

A good thing about dynamic memory is you can specify how much you want during runtime,
Example

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

void main()
{
	int *Pointer,size;
	cout << "Please enter how many integers you want: ";
	cin >> size;
	Pointer = new int[size];		//Specify how much memory I want.
	Pointer[0] = 56;			//Give the first element the value 56.
	cout << "Pointer = " << Pointer[0];
	delete Pointer;				//Important to delete it after use.
	getch();
}
Now here is where the user can specify how many integers he/she wants, hopefully not to much, else windows wont accept. Also when dealing with dynamic memory, dont use the elements which you are not sure will exist, eg: the user might type in 3, then you can not use the 4th element upwards. The main diference when dealing with new arrays, is that now you do not use the star infront of the pointer, I dont know why, but its not important. Also if you want to access the next element, you still use a ++ after it, eg - Pointer++; now it would point to the 2nd element(index 1).

ASCII
ASCII is short for American Standard Code for Information Interchange. Each key on the key board has its own ASCII value, each time you press a key, the keyboard sends an ASCII value to the computer. Keys which have 2 values - which you can press shift plus that key, have 2 ASCII values, eg: each letter has a lower and upper case, thats 2 ASCII codes, the number 1 can also be ! when pressed with the shift key, so it also has 2 values, where as keys like Esc only have 1.
Some common ASCII codes to remember are - Esc=27, Enter=13, F1=59, Down=80,Up=72,Right=77,Left=75. How do you use these ASCII values you ask, well we use that old function - getch();, like this - char key; key=getch(); now what ever key is pressed, the variable key will hold the key pressed in its physical representation, like g or something, but also its ASCII value. So now we could use if statements to find what one it was, eg - if(key==27).... this will be executed if the user pressed Esc (27).
Example

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

void main()
{
	char Key;
	cout << "Press Esc to exit." << endl;
	while(Key!=27)
	{
		cout << "Press any key to get its ASCII value: ";
		Key=getch();
		cprintf("%d",Key);		//Outputs ASCII value rather than character.
		cout << endl;
	}
}
Here if the user presses Esc, the program will finish, else it prompts the user for a key, then by using the cprintf("%d",Key); I can get it to output an integer instead of a character, which will output the keys ASCII value in decimal(normal scale). Also note that when you press a function key - non character (eg: Esc, arrows), It will return a 0 before the actual value, that does not affect the if statements or anything, but is just there to say that its a function key. Using the technique to get the keypresses, we can now have the user using the arrow keys, enter, Esc etc, this is very usefull.

Now lets try the Quiz - Click here


Back Home Next