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

Lesson 5



Classes
Clases are very similar to Structs, they were invented only for C++ and C does not have them, They have a private and a public part to them compared the the struct which was all public.
The private part of it, can only be accesed through the class itself, not by using something.something=blabla
The public is just the same as a struct, as far as i know, this is used to access the private bits, used kind of like a trigger or buffer to alter the private bits.
Example:

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

class My_class
{
private:
	int Age;
	char Name[30];
public:
	void Set_values(int a,char b[10])
	{
		Age=a;
		strcpy(Name,b);
	}
};				//Dont forget the semicolin

void main()
{
	My_class Person;
	char Temp_name[10];
	int Temp_age;
	cout << "Enter your age: ";
	cin >> Temp_age;
	cout << "Enter your name: ";
	cin >> Temp_name;
	Person.Set_values(Temp_age,Temp_name);
	getch();
}

Here I defined a simple class, and called it My_class. By default the class is private anyway but I still put Private: to make it clearer. I defined Age and Name within the private part. Note: normally the main variables go into the private part. Then those variables can only be accessed by the class itself, ie: the public part. In this part you should always have 1 or many function(s) which access the private variables. In mine, I sent Age and name to the function Set_Values(int a, char b) which were then tranfered to the private part by the function. I defined an instance of the class as person, then the rest normal exept the last line , to call a function in the class use - Instance name (ie:Person) then dot then the function call as usuall and send variables as neccessary, shown above. Now to get to the private part, was tricky thats why we use pointers to access it more directly.

Pointers
Pointers are a dynamic variable, I know they are used greatly in advanced programming. They point to a location in memory, which is used by another variable usually. To declare a pointer, put a star infront of it, just the same as other variables, but use a star.
Example

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

void main()
{
	int Number, *Pointer;
	Number = 5;
	Pointer = &Number;
	cout << "The value of pointer is: " << *Pointer << " At memory address: " << Pointer;
	getch();
}
The Pointer = &Number tells the pointer to point to the memory location used by Number as the & symbol stands for "memory location of". The pointer is not actually pointing to the variable's value 5, but the variable Number's memory location, something like - 1700. With pointers we can change what is stored in that particular memory location, like this - *Pointer = 25;. When you want the value of the Pointer, you use *Pointer, when you want the address of it, you use Pointer with no star. Of course you dont have to name it Pointer, but this was just for demonstration.

A bit on memory - Integers use 4 bytes, lets say that each byte uses one memory location, therefore each integer uses 4 memory locations, also the memory location of variables change every time the program is run, so dont use these memory locations directly. An example of memory is shown below -

int a=5,b=10,c=57;
Variables a b c
Values 5 10 57
Memory location 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
Click the variables for an explanation on each

Now if we declare a pointer to the variable a like this

int a=5,b=10,c=57,*Pointer;
Pointer=&a;

it would look like this in memory -

int *Pointer, a=0,b=10,c=57; Pointer=&a;
Variables a b c
Values 5 10 57
Memory location 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
Pointer --------------------------^
Click the Pointer for an explanation

The pointer points to the 1st memory location of the integer a. Now if we wanted the pointer to point to b, we could say Pointer=&b; but what becomes very usefull is that we can just put Pointer++;. This you would expect to increase its value by 1, but it increases the memory location by 1, althrough because its an integer it jumps up 4 memory locations. An example of this -

int a=5,b=10,c=57,*Pointer;
Pointer=&a;
Pointer++;


Pointer++ example
Variables a b c
Values 5 10 57
Memory location 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
Pointer -----------------------------------------------------------------------^
Click the Pointer for an explanation

Yes now the pointer points to the 1st memory location of the 2nd integer - b. But this would be too good to be true, as variables are probably stored in random or completely different memory locations, so you may end up pointing to the video memory or something and this is bad. But 1 thing that is sequential in memory is arrays. So if we point to the 1st element in an array ie:0, then we can use Pointer++; which will then make our pointer point to the next element in the array ie:1.
Example

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

void main()
{
	int My_array[10],*Pointer;
	Pointer=&My_array[0];		//Point to element 0 of the array.
	My_array[0]=4;
	cout << "Element 1: " << My_array[0] << " The same as: " << *Pointer << endl;
	My_array[1]=65;
	Pointer++;			//Note: adding 1 to its memory location, not value.
	cout << "Element 2: " << *Pointer << " The same as: " << My_array[1] << endl;
	getch();
}

Output
Element 1: 4. The same as: 4
Element 2: 65. The same as: 65

Now Pointer points to the 2nd element of the array, as array elements are stored sequentially (one after the other) in memory. This is actually a very simple program, I declare an integer array, and a pointer. Then I point the pointer to the 1st element in My_array[10]; which is index 0, then I assign the value 4 to element 1 (index 0). Now I ouput element 1, and output the value in memory which pointer points to, note the star infront of pointer as I want its value in memory not address. I compared them in the output, hopefully it should be the same. Then I did the same for the 2nd element, but I just made the pointer point up 1 memory location which just happens to be the 2nd element, note arrays are always stored sequentially in memory, then I output *Pointer and My_array[1], and they should hold the same value.

Classes and pointers
Now if we want direct access to a class's Private variable, we simply use a trick, call a function in the class and get it to return the Private variables memory location and then point to it.
Example

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

class My_class
{
private:
	int Age;
public:
	void First()
	{
		Age=0;				//Initialize variables
	}
	int * Get_location()			//Note: int * means im returning an address.
	{
		return &Age;
	}
	int Get_values()
	{
		return Age;
	}
};

void main()
{
	My_class Person;
	int *Pointer;
	Person.First();
	cout << "Age is: " << Person.Get_values() << endl;
	Pointer=Person.Get_location();		//Point to the address of Age in the class
	*Pointer = 25;				//Now we have direct access to Age!
	cout << "Age is now: " << Person.Get_values();
	getch();
}
First initialize the variable Age, then next I made a huge breakthrough with C++, before it would not return the address of Age - &Age because it was not an integer, but by trial and error, I found by putting a * 1 space after the int and 1 space before the function, you are able to return an address!!
Then i Pointed to that returned address with Pointer. Then I set the "Value" in memory which the pointer points to, to 25 not the address as that could be very bad. Then I output Age again, and this time it should be 25. Amazing?
Try the next Quiz - Click here

Back Home Next