storm@cs.mcgill.ca (Marc WANDSCHNEIDER) (06/16/91)
In the TP 6.0 Users manual, they start building up OOPs with objects typed Location and Point. However, on page 89, they give the following declarations: type Location = object X, Y: integer; procedure Init(InitX, InitY: integer); function GetX: Integer; function GetY: Integer; end; Point = object(Location) Visible: Boolean; procedure Init(InitX, InitY: integer); procedure Show; procedure Hide; function IsVisible: boolean; procedure MoveTo (NewX, NewY: Integer); end; MY problem is: Why have they declared an INIT procedure for type Point, when Location already has this procedure declared. If objects have inheritance, then there should already be an INIT procedure built into the Point type. Any Insight...>? ./*- storm@bart.cs.mcgill.ca storm@sizone.UUCP -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ storm@cs.mcgill.ca McGill University It's 11pm, do YOU Marc Wandschneider Montreal, CANADA know what time it is? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dmurdoch@watstat.waterloo.edu (Duncan Murdoch) (06/17/91)
In article <1991Jun16.075044.18283@cs.mcgill.ca> storm@cs.mcgill.ca (Marc WANDSCHNEIDER) writes: > >In the TP 6.0 Users manual, they start building up OOPs with objects typed >Location and Point. ... > >MY problem is: Why have they declared an INIT procedure for type Point, when >Location already has this procedure declared. If objects have inheritance, >then there should already be an INIT procedure built into the Point type. In the example you gave, you are correct: though declared as constructors, the two Init routines are just procedures. However, in more complicated cases, it's essential that an object have its own constructor, because it's the constructor that "stamps" the object with its dynamic type. If you had virtual methods in your Point, but called the Location constructor to initialize it, you'd get the Location virtual methods, not the Point ones. This causes problems sometimes. If in your Point constructor, you make a call to an ancestor's constructor without giving the type (impossible here, because they both have the same name) it'll "transmute" your object into the ancestor type, and you'll end up very confused later. I hope this helps a bit. Duncan Murdoch dmurdoch@watstat.waterloo.edu
bdhelm@immd4.informatik.uni-erlangen.de (Bernd Helm) (06/17/91)
dmurdoch@watstat.waterloo.edu (Duncan Murdoch) writes: >In article <1991Jun16.075044.18283@cs.mcgill.ca> storm@cs.mcgill.ca (Marc WANDSCHNEIDER) writes: >> >>In the TP 6.0 Users manual, they start building up OOPs with objects typed >>Location and Point. >... >> >>MY problem is: Why have they declared an INIT procedure for type Point, when >>Location already has this procedure declared. If objects have inheritance, >>then there should already be an INIT procedure built into the Point type. >In the example you gave, you are correct: though declared as >constructors, the two Init routines are just procedures. However, in more I think, you missed the point: Point.Init is needed to initalize correctly the Visible-variable in Point where Location.Init doesn't know anything about Visible: >> Point = object(Location) >> Visible: Boolean; ^^^^^^^^ >> procedure Init(InitX, InitY: integer); I would implement Point.Init so: procedure Point.Init(InitX, InitY: integer); begin Location.Init(InitX, InitY); Visible := FALSE; { do what is needed to show the point... } end; bye,bye Bernd