Gwerdy Logo

[ FAQ | Privacy Policy | Links | Search ] Really Simple Syndication

Article :: Introduction to Object Orientated Programming With Jamagic - Part 1 <<

 

Gwerdy Details


» News
» About
» Contact Us

Gwerdy Products


» SuDoku Solver
» Moo Java
» Gwerdy Wordy
» Number Cruncher

:: Under Development ::
» Gwerdy Chat

Gwerdy Services


» Web Site Design
» Contract Programming

Archive Material


» MMF Archive
» Jamagic Archive
» Free Download Games



 

:: 3. Member Variables

That was a totally useless example, objects need to store some data at least. Let's make a object "Dog" which stores two pieces of data, the dogs name and it's age. Data stored within an object is often called a member variable. These variables will be passed to the "constructor" as parameters in the object creation line so that when we create a "Dog" object we also have to tell our constuctor what values we want to store in this dog's name and age member variables.


//***************************************************************************
//****  Ex 2 - Adding Member Variables to Objects   *************************
//***************************************************************************

winMain = New Window(640,480);

// Lets make a Dog object!
dog1 = New Dog("Fido",2);

While(TRUE);


//***************************************************************************
//**** Below is all the code for a user object - Dog ************************
//***************************************************************************
Function Dog ( dname , dage )
{
	//*** This is our dog object. This function runs whenever we create a new dog object
	//*** This is sometimes called the "constructor".

	//*** Lets give the dog some properties set by the user when they create a dog
	//*** These properties/variables are often called "member variables"
	This.name  = dname;
	This.age   = dage;
}
//***************************************************************************
//**** End of Code for object - Dog  ****************************************
//***************************************************************************



Ok, you pasted or copied all that and it did nothing right? Wrong! if we take a look at the debugger you will see:


The Debugger


Congratulations you have created your very own "Dog" object which has the variables name and age. The "Dog" object has been stored it in the pointer 'dog1'. So could we have just created two basic variables called say dog1_intAge and dog1_strName intead of bothering with all this OO business? Yes, but this is the design model used in the procedural approach and as you will hopefully see using the OO approach has many advantages.

Now add the following lines of code to your example, below the first 'dog1' object creation line of code, but before the while(TRUE):

dog2 = new Dog("Butch", 10);
dog3 = new Dog("Amber", 3);


The Debugger


We have three dogs now, again you may say I can do that with six variables. But imagine you had ten dog objects using the old approach then decided you wanted to add a new member variable "weight", that would be a lot of work. Whereas in the OO case its far less effort. Hold out a little longer and see more reasons why the OO method is considered superior.

First lets change how we store the pointers to the dog objects, instead of storing them in variables called 'dog1', 'dog2' and 'dog3' there is no reason we cant use an array.

Replace the following code:

dog1 = New Dog("Fido",2);
dog2 = New Dog("Butch", 10);
dog3 = New Dog("Amber", 3);

With:

objDog[0] = New Dog("Fido",2);
objDog[1] = New Dog("Butch", 10);
objDog[2] = New Dog("Amber", 3);

What we have done is create an array called 'objDog'. Warning: Do NOT call the array the same name as your object (i.e. in this case 'Dog') because it will in Jamagic cause issues as the object itself is called Dog!

In this array we have created three dogs at the array locations 0,1 and 2. The reason for this is we can then loop through the dog objects no matter if there are three or three million with the same code, we can not do that if they all not part of an array like in the initial example.


The Debugger


Jamagic differs from other OO languages such as Java and C++ in that you can access the member variables of an object directly (i.e the age and name in our case) Other languages normally require you to tell them if the variables can be accessed directly (public) or can not (private). In Jamagic all member variables are public!

How do we access them? We use the dot (".") operator.

Add the following code below the three dog objects. It will loop through all our dog object in the array and print to screen their name and age.

For( i = 0 ; i < 3 ; i++)
   CurWindow.WriteLn( " Hi I am " + objDog[i].name + " and I am " + objDog[i].age + " years old!");


Program Output


You can (because member variables in Jamagic are public) even change the member variables within the object using this same method. To change the age of Fido say you would use the following:

objDog[0].age = 5; // We have changed the age variable!

This is not the best way to do things because we said design and implementation should be separate but in this case if the programmer of the dog object, which in a big project might not be someone we know, changed the way the object works our program might fail. For example instead of age the object stored the date of birth (and then had code to calculate the age based on the current date) the new updated dog object would no longer function in our program. A better method to avoid problems such as this is to use something called "Instance Methods" which will be explained next.

This direct method of accessing member variables is faster (4x write, 2x read in my simple test) so it is up to you on which method to use, the safer and cleaner accessor method we will see next or the faster direct method shown above. If the source code is being provided for other's to use then you will almost certainly need to use instance methods.



» Next : 4. Instance Methods

 

:: Recommended Book


Recommended Book
Elementary Game Programming and Simulators Using Jamagic


Index:
1. Introduction
2. Simple & Useless Example
3. Member Variables
4. Instance Methods
5. Accessor Methods
6. Conclusion

 


[ FAQ | Privacy Policy | Links | Search | Top]

© 2005-2007 Gwerdy Software. All Rights Reserved.