[Tutor] Classes!Classes!Classes!

FFlores fflores@arnet.com.ar
Tue, 21 Dec 1999 10:51:49 -0300

R Parker <spiderman@nasachem.com> wrote:
> I have studied and studied from books, tutorials and other such
> materials and have yet to figure how to use classes and what they do.

OK, I'll try, since I'm just back from an OO Analysis final.
(You don't want that!).

In OOP (Object Oriented Programming) you organize all data and
behaviour inside objects. An object can represent a concrete
physical object, or an abstract entity. Examples of objects
that can appear in a program: File, HtmlPage, List, Tree,
Person. Each object has some data (attributes), like (for a File)
name, size, descriptor, position of the pointer, opening mode
(R/W etc.). It also has responsabilities, things that it must
do when asked -- these are implemented by methods, which are
like functions except they are inside the object (e. g. for a
File, read(), write(), seek(), etc.).

Now, you first identify the objects you need in your program.
If you're programming an html page generator, you could have
objects like HtmlPage, UnorderedList, Header, etc. But you
don't have to specify the structure of each object; you define
a class per each type of object you need. Each class is like a
pattern for you to produce objects. After a class is defined,
you can call it (object = ClassName()) and it will return an
object (also called "instance" of the class).

Objects are more useful than common variables because you don't
have to link functions and values; the object is self-contained
and knows what it can do. This saves you from type checking, and
lets you do something called polymorphism, which is calling
objects of different classes with the same method name, provided
both can reply to it after their own fashion.

Besides, with classes you can have inheritance. Suppose you have
a class named Person, with attributes name, date of birth, social
security number, etc. Now, if you need a class named Teacher, which
has all the attributes in Person but also some extra ones like
a list of courses, you can derive it from Person; it will inherit
all attributes and methods, and you can define new ones. To do
this you use

	class Teacher(Person):
	<class definition>

You can further derive more classes, creating a hierarchy. In the
end you could have a lot of object classes, and your program will
be composed of objects which call each other's methods, after being
created and initialized by the main routine.

Hope this helps...

--Pablo Flores