|
|
What you are going to learn today:
You've heard the hype, now learn the real thing. As you'll soon find out, Objects are just extremely functional ways of organising information. However, don't expect to learn everything. After all, you still want your sanity after this, right? Topics like polymorphism and virtual functions don't even get a single sentence here! What is Object Orientated Programming?Like all other programmers, I shall use the analogy of a watch to explain the concept of objects. A watch is like a record, isn't it? It has values like the current date, the temperature. and of course, the time. In a record declaration: type
TWatch = record of
date: TDate;
temperature: TTemp;
time: TTime;
end;
Now, a watch not only stores values, it also has procedures and functions built
inside them. For example, each time you press a button, a procedure is run to change
the display. Or maybe the procedure changes the time. We don't know. But you've got
the idea don't you? An object is just a lot of things put together into one. OOP
scientists like to call this encapsulation.
But what makes an object so special? Well, look at it this way. Imagine that you're going to make a new watch which is exactly the same as the old one except that it can show the compass directions. (Yes, there are such watches around.) Instead of re-typing everything, what objects allow you to do is to inherit from already existing objects. That is, to make a new object from an existing one ane modify it. Useful, isn't it? Another feature of abjects is its ability to protect variables. This feature prevents procedures from modifying values they shouldn't be touching. For example, the watch wearer may want to change how long a second is. However, because you have protected it, the user has no way to modify the length of the second. On the other hand, if the watch, for some unearthly reason, wants to shorten the length of a second, it can be done - that is if you allow them to of course. How is OOP implemented in Pascal?Well, we originally planned for Delphi to be the 'teaching tool' for this chapter, but implementing it gave some problems. So, instead we'll be using Turbo Pascal instead. Delphi's OOP implementation has also changed from Turbo Pascal's, so don't expect the code to be portable!But never mind - to program OOP in Pascal, you must first create a class. A class, which represents the watch in our analogy, can be inherited into another class. A class also encapsulates other things, like how Class A encapsulates Days 10, 11, 12, and 13. Want to see how a class definition looks? Well, here it is: type
TThing = object
public
i, j: integer;
end;
But that's only the simplest implementation for a class (also known as an object) -
in fact, not only can you place values inside classes, you can also place procedures
and functions! Here's an example:
type
TThing = object
public
procedure WatchMeCry;
end;
procedure TThing.WatchMeCry;
begin
Writeln('Waaahhh!');
end;
Huh? Don't worry if you don't understand it. We'll explain it to you bit by bit.
Let's look at the first example first. Like a record, it stores two fields -
i and j. See it's structure is just like a record? But wait! What's that 'public'
doing there? Well, it's there to define what type of 'visibility' the fields
after it will be. The different keywords are:
begin
Thing.i := 7;
Thing.j := 1;
end.
Yes, you use the same dot operator you used with records. See the similarities now?
An object is just like a record in almost every way. Ahh, good news for us. We can
skip over 40% of this introductory stuff now...
But how about our second example? About declaring procedures and functions? Actually, it's very simple. When you're declaring the class, just add a procedure statement which is the same as the procedure statement you type when declaring the procedure. For example: type
TThing = object
public
procedure WatchMeCry(Length: integer);
end;
That line is just there tell Pascal that procedures exists - but at the moment it'a
undefined. Now, how do we give the procedure some code to run? Simple, we would have
to declare it later in the code with the name of the class, the dot operator,
then the name of the procedure. Just like this:
procedure TThing.WatchMeCry(Length: integer);
var
i: integer;
begin
for i := 1 to Length do
Writeln('Waaahhh!');
end;
See the name of the procedure? Now you get what I mean. The method to implement
functions is the same too. So, you can actually call up a function just like this:
(Magic, actually, magic...)
var
Thing: TThing;
begin
{ Prints 'Waaahhh!' 5 times }
Thing.WatchMeCry(5);
end.
Bet you've never seen anything like that before! OOP is pure power, but hold on -
it's not even starting yet! (If it was that simple, we would have put it with the
part on records instead of wasting so much time typing.) Objects are more than what
you've just seen.
For instance, can you inherit a record? Well, for classes, the answer is yes. To inherit a class from another, place the ancestor class between brackets after the keyword 'Object'. See this? type
TGone = object(TThing)
public
Zoo: integer;
end;
What have we just done? Well, we have just created a new type TGone which is the same
as type TThing except that it adds a new field - Zoo. Remember what are the aims of
inheriting? It's not just for adding new functions, you know. We can also modify
existing ones:
type
TGone = object(TThing)
public
procedure WatchMeCry;
end;
procedure TGone.WatchMeCry;
begin
Writeln('Crying Out Loud!');
end;
As you can see, as long as you make a new method which has the same name as an
existing one on an ancestor class, it will completely replace it - including the
parameter list. Quite powerful, eh? Now let's have a concluding example program
to round it all off, shall we?
1: program ObjectFolly; 2: 3: type 4: TThing = object 5: public 6: Money: integer; 7: procedure KillMoney; 8: end; 9: 10: var 11: MoneyThing: TThing; 12: 13: procedure TThing.KillMoney; 14: 15: begin 16: Money := Money - 10; 17: end; 18: 19: begin 20: MoneyThing.Money := 100; 21: MoneyThing.KillMoney; 22: Writeln(MoneyThing.Money); 23: end.
90
Oh, that was fun. Now that you've got the basics, we can ask this very simple question: How is Delphi Object-Orientated? Well, the answer's right below. How is Delphi OOP?When you first start up Delphi, do you notice that there's something which looks like this at the top of the code listing?type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;
Believe it or not, that's an object! And if you know Delphi well enough, you'll
know that everything in Delphi, a label, a button, anything, is an object by
itself! Everything! Utterly everything!
The End Of Day 12Yes, we've covered OOP, and now it's your last day. Ahhh, this is going to be sad. But hey, you're gonna leave anyway, so why not finish the course first? Day 13 is there for you - Go on and take that final step! Bye!email: tq97-11127@advanced.org |