jeffb@yang.earlham.edu (Jeff Braun) (10/15/90)
Hello, I'm very new to Turbo Pascal 5.5 and as well to Object-Oriented programming in general and I have a few questions if anyone could help me: 1. What is the purpose of using the reserved word CONSTRUCTOR? I understand that every "...object that has virtual methods must have a constructor" (Turbo Pascal: Object-Oriented Programming Guide, page 26) but I don't seem to understand what it is telling the compiler? I sort-of understand that it's used for intialization of a object with virtual methods (right?), but what does it initialize and can they be used for anything other than intialization? So if I have an this piece of code taken from page 42 of TP:OOPG: Location = object X,Y : integer; procedure Init(Initx, Inity :intger); function Getx: integer; function Gety: integer; end; PointPtr = ^Point; Point = object(Location) Visible : boolean; constructor Init(Initx, Inity : integer); procedure Show virtual; procedure Hide virtual; end; Is it used in constructing the X and Y variables in Point or is it allowing for PointPtr to be Initialized at run time as an object pointer? 2. What about DESTRUCTORS? Are they like DISPOSE in Turbo Pascal? I realize these are very simple questions, but I'm just a little (maybe a little more than a little) confused. Thanks. Jeffb@yang.earlham.edu
trodgers@nmsu.edu (Thomas Rodgers) (10/17/90)
The constructor is required because virtual methods do not know precisely which instance of the method is to be called until the constructor code initializes a virtual method table within the object, there is a discussion of the VMT in the reference section of the OOP guide. Also only a constructor can be specified in dynamic allocation of an object (I think) (ie. new(Some_Obj,Init(X,Y,BLAH)). The same holds for the destructor, so that when you deallocate an object, dispose(Some_Obj,Done) performs any cleanup the object needs before it is disposed of.