==============================================================================
C-Scene Issue #04
Object Oriented Programming Intro
Noah Roberts
==============================================================================


	Object Oriented Programming (or OOP from now on) is an approach to
program design.  It can be used in most languages, but works best in one that
was meant to be OOP, like Java, C++, or Objective C.  It is an entirely
different concept from 'procedural' programming, which languages like C and
BASIC are.

	So, lets look at the differences first.  In procedural programming we
program based on what the program does and when.  Most of the time, we have
variables and functions that are global (this is in C), the functions of
course have their own variables that are localized, but besides that,
everything is system wide.  This is quite different from OOP.  In OOP we are
programming based on 'Objects', which I will explain in detail later.  Each
object has its own methods(functions) and variables.  Most variables are not
global, though in some languages, method names are (it is important to realize
however that in OOP we can be sure what object is being told to perform that
method).

	One of the first steps in understanding OOP is to know the grammar.
This helps you to think in terms of objects instead of procedures.  To begin
with, each object should be a noun.  You wouldn't call a cat a meow would you?
This is because we are building a thing, not an action.  This is about the
most important thing in the OOP approach.  Each object then contains actions
it can perform, and these should all be verbs, as would be in normal English.
In some languages, like Java, there are certain methods which are not labeled
verbs, but we will deal with that difference later.  Then we have what are
called instance variables, these are 'object globals' which are a part of
every instance (occurance) of that object.  Think of these as an integral part
of a thing, like your hand is a 'part' of your body (though you hand is
obviously not a variable :).

	Lets build an object, something simple...a string.  In regular C, you
would have functions that are global, that deal with char pointers in
different ways.  You could put these in a separate file, but if later you
forgot and called some other function the same name,...things could explode.
In OOP, and objective c, that is not a concern.

	So, what do we want this string to be?  Well, keeping it simple, we
are going to just create a char pointer.  You may ask, what is the point?
well you will see...  Being a char pointer what exactly are its parts?  Well
it has characters and a length...so our instance variables will have to be
just that.

{
	char *string;	/* All the syntax in this intro is done
	int length;	 * in Objective C, but the concepts apply
}			 * to all OOP languages. */

	Now we need to be able to define this object.  What string is, and how
long it is.  So we will want to be able to tell it to put data there.  Here
are a couple of methods this object could perform...
(In Java, this is usually done by a 'constructor', a method which is called the
same thing as its class object and is called automatically when you create a
new instance of that object. I believe this is also the case in C++ and maybe
several others.)

-define:(char *)what
{
	// stuff to make string = what and length = strlen(what)

  	return self; /* in objc always return self, and return this object.
		      * unless your want something else. */

} // actual name if this method is "-define:"

	We could also make is possible to send 2 strings that will be merged
and put in string.  We can name the method which does this, the same as the
method that only takes 1 arguement.  This is called argument overloading.

-define:(char *)first :(char *)second
{ stuff to do stuff
}
 the actual name of this method is "-define::"

	Easy enough...now what do we want to be able to do with this object..
Well, we want to be able to get string and length back from it, so lets build
a couple of simple return actions

-cString
{
	return string;
}
-len
{	return length;
}

	We could also have some actions to copy the string and give it to us
so that our new string doesn't effect this object (pointers are still pointers
and do still point to the same location ;)  but thats for you to build later
and has no real impact on showing you OOP.

	Ok, what else do we want it to do?  How about compare this string to
another?  Easy...

-equals:(OurString *)theStringToCompare
{
	if(!(strcmp(string, [theStringToCompare cString]))
		return YES;
	return NO;
}

	And we do not have to worry about are call to
				[theStringToCompare cString]
	is going to call our own cString method, because that 'message' is
	being sent directly to theStringToCompare and only it recieves it.

	Ok, we would undoubtable want this object to do a lot more, but that
is enough for us to go by.  This is also not an entirely complete object.  We
are going to pretend it is though, since the only things we would need to do is
put this together in a file and define the Object in whatever way we need to in
the language we chose.

Lets build a program that uses this object.

int main(void) {

	/* This is the only procedure we will be building
	 * and as you may notice, it is also very different.
	 * a lot of times, the main() function will only create
	 * the first object, which will take over from that
         * point on.
	 *
	 */

	/* lets make a new string object */
	OurString *str1 =[[OurString alloc] define:"Hello World!\n"];

	/* alloc is an action that OurString inherited, which I will get to */

	OurString *str2 = [[OurString alloc] define:"Not Hello World!\n"];

	/* now lets ask str1  if it is the same as
	*  str2.
	*/
	if([str1 equals:str2]) {
		printf("yes\n");
	else
		printf("no\n");

	return 0;
}

	This of cource does nothing but print "no\n" to the screen, but it is
not meant to do any great thing but show you about objects.

	Objects can also contain other objects, and send messages to them
directly.  We do not need a main function to do that, it is just the way all of
the OOP languages I have used start the program.  It is not a good idea to let
main do much, if anything, besides initialise your program and exit.

	

Inheritance....

	I feel this topic is important enough to deserve its own section.
This allows you to write Objects(classes) which be perform a basic set of
actions, and then extend those object so that they are now more specific in
nature.  Instead of rewriting the entire set of methods and variables, you just
write a'subclass' of the original which 'inherits' all the stuff the original
(or 'super') class had.  It is important to realize that the inherited items
are actually now a part of the new class, and not just being called by the
super.

	Like say for instance we wanted to be able to also edit our string.
Like remove a character at a certain point in the string.  What we would then
do is subclass OurString and add the method:
-removeCharAt:(int)were
{stuff to remove the char
}

Makes life a lot easier doesn't it.

	Classes inherit from their super-classes, all the way up the tree,
meaning, if we sub-classed our new subclass of OurString, the new object would
not only inherit from its direct super, but from OurString, and any superclass
OurString has.

	We can also redefine methods that we defined in the superclass to do
different stuff.  This is as easy as re-defining it in the new class.  If you
then need to call the old method you can, by calling super.

	Most languages, or libraries, contain a super-super class wich all of
the classes you create should be a subclass of.  Most of the time, this class
is called 'Object' or something like that.  You should become familiar with
your languages super-class because all classes you create will have the same
methods and variables as Object by default.

Encapsulation....

	This is an easy concept that is often described in a confusing manner.
All it really means, is what I said before, all of your objects methods and
variables are part of the object, not a global function or variable like in
procedural programming.  You can let some of them be visible, or not, most
languages (if not all) have ways of defining how visable an objects innards
are.

	This ties in with 'messaging' which is another important, but sometimes
confusing thing.  Objects respond to messages they are sent by other objects.
If we want to ask OurString if it is equal to another string, we send a message
to it asking it if it is, it then checks itself to see what message it should
send in reply.

Goodbye...

	I hope I have helped any newbie Object Programmer to get past the
initial state of confusion.  OOP is a very natural way to think when it comes
to writing complex programs (or even simple ones).  In my opinion, it makes
more sence most of the time to use OOP instead of procedural.  At first the
concept may seem overwealming, but soon a light will click in your head and it
will all make perfect sence.

	I have not but touched on a few of the concepts I felt most important
or confusing to a newbie.  You should also read some of the googles of
tutorials and FAQs on the net.  You can find a good intro to OOP and
Objective C (teaches both the language and the concept) at the Rapsody
docs here: http://developer.apple.com/techpubs/rhapsody/ObjectiveC/index.html
it assumes you know procedural programming, and especially the C language, but
assumes you know nothing of OOP, good for beginer and intermediate.



This page is Copyright © 1997 By C Scene. All Rights Reserved