Object Oriented Programming Philosophy

Part 2 -- Constructor and Destructor




Welcome

Now, you should have understood the concepts of encapsulation, instantiation, and  whether public or private. This time, we are going to discuss about inheritance, constructor, and destructor.

 

Constructing Method Body

Constructing method body is just like constructing procedures or functions. Consider the following example:


type
  pMyClass = ^tMyClass;
  tMyClass = Object
               private
                   x, y: integer;
               public
                   procedure setX(newX: integer);
                   procedure setY(newY: integer);
                   function getX: integer;
                   function getY: integer;
             end;

:
: { other classes declaration and/or variables and/or other procedures/functions }
:

procedure tMyClass.setX(newX: integer);
begin
  x := newX;
end;

procedure tMyClass.setY(newY: integer);
begin
  y := newY;
end;

function tMyClass.getX: integer;
begin
  getX := x;
end;

function tMyClass.getY: integer;
begin
  getY := y;
end;

:
:
:

begin
 : { your main program here }
end.

The method body will of course have to be declared BEFORE the main begin...end block. However, the normal scope of procedure calling between one object methods does not apply. It is because all method headers are already defined in the object declaration. So, suppose there is a procedure foo placed after the function getY above, the procedure setX can call foo (if necessary) despite the fact that foo is below it. The scope rule for other procedures or functions (other than OOP ones) does apply, however.

 

Constructors

Naturally, when we want to create an instance of an object, we would like to initialize some things (like assigning variables to some values, allocating some memory for buffers, etc) prior to utilizing the object. The OOP has this. We would have to make a constructor to do it. What is a constructor? Constructor is basically a method, specially invoked during the creation of the object instance. To make a constructor in Pascal is very simple just declare a method with a keyword constructor. That's it. Usually the method name is Init, but you can name it your own. Just keep in mind though, that Pascal's creation of constructor is somewhat unusual compared to other languages like C or C++.

Suppose you have the code of class declaration of tMyClass above (retyped here for convenience):


type
  pMyClass = ^tMyClass;
  tMyClass = Object
               private
                   x, y: integer;
               public
                   procedure setX(newX: integer);
                   procedure setY(newY: integer);
                   function getX: integer;
                   function getY: integer;
             end;

:
: { Method body declaration as above }
:
var
  ins : pMyClass;

when you create a new instance in ins, by doing new (ins);. If you forgot to initialize the variable x and y, and you directly call the function getX, the answer would be unpredictable, right? So, I suppose that you would like to make the variables hold some default values. Here goes the use of constructors. When you add the constructor, the object declaration should look like this:


type
  pMyClass = ^tMyClass;
  tMyClass = Object
               private
                   x, y: integer;
               public
                   constructor init;
                   procedure setX(newX: integer);
                   procedure setY(newY: integer);
                   function getX: integer;
                   function getY: integer;
             end;
:
:
:
constructor tMyClass.init;
begin
  x := 3; y := 5;
end;

:
: { Method body declaration as above }
:

var
  ins : pMyClass;

Here, we would like to initialize the x to 3 and the y to 5. Thus, the hazard is avoided. However, when creating instances of object that has a constructor, you will have to invoke the new along with the object name and the constructor name. So, instead invoking this way: new (ins);, we would have to invoke it this way: new (ins, init);. Of course you can have multiple constructors in one object. When you would like to create an instance, make sure you call the correct constructor, too.

Constructors behave much like procedures: it does something and it returns nothing. It can also have some parameters so that we can pass some value or reference to it. Yes, of course we can modify the constructor init into this:

constructor tMyClass.init(startX, startY: integer);
begin
  x := startX; y := startY;
end;

Of course we have to modify the init method declaration into this:

constructor init(startX, startY: integer);

Instantiating the object, is similar: new (ins, init (3,5));. This pass the value 3 to startX and the value 5 to startY.

 

Destructors

Destructors behaves much similarly as constructors. Constructors are called during the creation of the object, but destructors are called during the destruction of the object. The purpose of having destructor is to destroy any memory allocation created by constructors. Suppose you have a constructor that allocates some memory for a buffer and store it in pointer p. After the object get destroyed, you have to destroy p as well. Otherwise, that portion of memory will floating around and occupies some space unused. The destructor then come in, invoked when the object got destroyed and clear such memory allocations.

The declaration of destructor is very similar. The name picked for destructors are usually Done. The declaration inside the object looks like this:

destructor done;

Of course you can supply some arguments if you'd like to. Then the destructor's body is somewhat like this:

destructor tMyObject.done;
begin
  :  { do some necessary deallocation stuff }
  :
end;

As you recall that we have the command dispose to purge the memory allocation requested by the command new. The destructor is called similarly. So, we can call dispose(ins,done); after things have finished.

Summary

To sum up, declaring method body is very similar to making procedure or functions. Constructor is just a special procedure that initialize things up. Destructors is a special procedure that clean things up. Now, you should go to the next lesson to comprehend inheritance.


Where to go

Chapter 3
News Page
Lesson 1 contents
Contacting Me


Roby Joehanes © 2000