[Tutor] constructors

alan.gauld@bt.com alan.gauld@bt.com
Wed, 10 Apr 2002 15:23:25 +0100


> My understanding is that a constructor is useful to make 
> sure that the object and its default values are initialised 
> properly.

That's the best reason. It ensures that the "pre conditions" 
of the objects methods are all met. Usually that translates 
to setting attributes but it could also be things like 
checking a hardware device was functioning correctly or 
even existed. This saves each and every function from doing the precondition
check itself. (Actually for hardware you 
probably should still check coz somebody might unplug it!)

It also means you the user get a check on whether its even 
worth attempting the operation.

Alan g.