Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucsd!sdcsvax!ucsdhub!hp-sdd!hplabs!hpfcdc!hpldola!hpctdlb!hpctdls!wei From: wei@hpctdls.HP.COM (Bill Ives) Newsgroups: comp.lang.pascal Subject: Re: experience with tp5.5? Message-ID: <390004@hpctdls.HP.COM> Date: 19 Jul 89 14:12:16 GMT References: <799@eutrc3.urc.tue.nl> Organization: Hewlett-Packard CTD, Colo. Spgs. Lines: 145 I just received my upgrade package and have read the chapter on OOP. I will try not to reelaborate what previous posters have said. Basicly, there are four new keywords and a couple of new functions that are used to support OOP. The new keywords are: object constructor destructor virtual The new fuuntions are really just new forms of new dispose The new proc can now take the pointer as the first parameter and a constructor as a second parameter. i.e. new(myptr,Init(0)); Here "Init" is the arbitrary but suggested name (by Borland) of the constructor for object of which myptr points to. Dispose has a similar form but with the destructor as the second parameter: Dispose(myptr,Done); The new routine can also be called like malloc is in C --- myptr := new(myobjtype); The "object" keyword is used just like the RECORD keyword is but instead defines an object, which can contain methods (procs and funcs) : type myobj = object x : integer ; { all vars MUST come first } constructor Init( i: integer);{ "constructor" keyword used like "procedure" } destructor Done; { note keyword destructor is used like "procedure" } procedure mymethod( var i : integer ); end; All methods associated with an object must be defined in the following form: procedure myobj.mymethod( var i : integer ) begin x := i ; { note assignment to myobj's x field which is possible since this is a member method of myobj } end; This code might be called like: var mine:myobj ; begin mine.mymethod(1); end. The constructor and destructor methods must be called when the object contains virtual methods or unpredicable result may occur. Even if the object is statically allocated and is then assigned to another object the new object must be initialized to ensure that its VMT (virtual ?memory? table) is properly connected -- there are some big warnings about this in the manual. For example if the myobj object above did contain the virtual method: procedure myvirt( i:integer ) virtual ; { syntax may not be right here} the following code demonstrates what initialzation is required: var myptr : ^myobj ; mine : myobj ; begin new(myptr,Init(2)); { allocates and inits the dynamic var } mine := ^myptr ; { assign dynamic var to the static var } mine.Init(2) ; { initialize the static var } end; The manual describes this is great detail -- for now beware. Virtual methods are used to allow descendant objects to replace parents virtual methods with there own. The following code not only shows this but also shows how inheritance is done: type aa = object i : integer ; procedure virt1; virtual; procedure test; end; bb = object ( aa ) { bb inherits from object aa } j : integer ; procedure virt1; virtual; end; procedure aa.virt1 begin writeln('hello'); end; procedure aa.test begin virt1; end; procedure bb.virt1 begin writeln('world'); end; var a: aa; b : bb ; begin { initialize both a and b since they have virtuals in them } a.test; { this will print out 'hello' } b.test; { this will print out 'world' } end. Virtual methods allow a descendant to override ancestor methods. Object can also be assigned AS LONG AS THEY CAN FILL THE TARGET. Thus a:= b ; would work above since b contains everything that a does plus the variable j. b:=a ; would fail at compilie time since a cannot fill in b's j variable. This same type of logic works for type checking and parameter passing: For the procedure: procedure mine( i : aa ); the following calls are legal: mine(a); and mine(b); For anyone familiar with C++ here are some notes: constructors and destructors are NOT called automatically like C++ there is no such thing as operator overloading only single inheritance is support ( ala ATT C++ version 1.2 ) not multiple inheritance (ala C++ 2.0) the standard OOP features such as polymorhism and encapsulation are supported through object and virtual keywords. method overloading appears to be possible if the methods are static and have different number/type of parameters ( I haven't tried this ) since pascal does not normally support the returning of records from functions I suspect ( it was not documented ) that they cannot return objects. data hiding (i.e. private vs public) seems to be absent from the object definition itself but can be supported somewhat with the UNIT INTERFACE IMPLEMENTATION usage. There appears to be no way to really hide data or methods in an exported object. Hope this helps those of you who are curious about TP 5.5 -- I hope it was worth the price I paid -- I don't know yet. Bill Ives Hewlett-Packard CTD include I do not represent Borland or speak for anyone but myself.