AJ wrote:
So, what I'm having difficulty with is what's the difference bewteen a class
and a function? A function to me looks like a piece of code in an external
file (possibly) that I can call when I need it instead of duplicating code
in different files. A class looks to achieve the same thing. Am I missing
the point somewhere?
You seem to be close with your definition of a function.
a function is a 'named' chunk of code.. it's called a function because
it *does something*.
(If you're unsure of the concept - think of it in real-world terms,
similar to the english dictionary definition of 'function'.. for
example, a function of your wristwatch is that it tells you the time...
another function of your wristwatch may be that it sounds an alarm.)
when you use a function by calling it's 'name' (known as "calling a
function") it executes the chunk of code.
as you guessed, a function makes this single chunk of code reusable
anywhere else in your code.
On the other hand, a Class is a completely different concept... In fact,
it's a horrible concept to try and understand what it is at first, and
even worse, try to understand *why* it's useful... But i'll try to
explain :-)
So.. first off, Forget programming for a second.. forget your project
and your code.. the best way (IMHO) to try and understand what a class
is, is to think about it in real-world terms.
Think of a "class" as a "blueprint". (for example, new houses on a
housing estate have a single blueprint that is used for all houses)..
for the english dictionary definition, it relates closely to
'classification'
a "class" is a "blueprint" for an object... in the same way that a
blueprint may be for a new house on an estate... Hopefully you
understand what an object is, else this may be a little confusing
already :-)
And see all those new houses on the housing estate.. They're all built
to the same specification (as per the blueprints)... The specification
of each of these houses includes properties/attributes (such as Colour
of front door, garden plants, etc ).. and some Functions (such as "turn
on the tap", "open garage door", "open the window")... but of course
these functions can only be performed on the specific house (I mean, you
don't turn on your tap and run your neighbor's bath do you?)
When all these houses are first built, they're all pretty much
identical, however, they are seperate houses which exist on their own
independently of each other - the only thing that links them is their
original specification/blueprints... Of course, when people move in,
they can be repainted, have different plants in the garden.. etc.
In the same light, all your objects which are constructed from your
class have the same properties/attributes (Data variables), and also
contain a bunch of functions that can be performed on the object
(usually to modify the data variables of the object).
For example. your class may be called RollOverHyperLink .. The
attributes being maybe IdlePicture, ActivePicture, height, width,
border, linkURL, alt-text etc.. you get the idea.. Functions could be
Set_To_Active, Set_To_Inactive.
Then you could have loads of buttons on your webpage that are Rollover
hyperlinks which are new objects based on the class RollOverHyperLink ..
each of them given unique properties which are appropriate to every
specific object (or 'instance').. one may have IdlePicture set to
billgatescat.jpg and linkURL set to
www.microsoft.com, and another may
have IdlePicture set to Tonyblairinstockings.jpg with linkURL set to
www.number-10.gov.uk ... etc.. Hopefully you get the idea. :-)
--
Ben Cottrell AKA Bench
Disclaimer:
This post may contain explicit depictions of things which are "real".
These "real" things are commonly known as 'life'! So, if it sounds
sarcastic, don't take it seriously. If it sounds hazardous, Do not try
this at home or at all. And if it offends you, just don't read it.