5.3 Virtual Procedure Declaration


Through class inheritence, procedures can be called with either the type of the inherited class or the type of the class that inherits it. A virtual procedure allows an inherited procedure to be overridden in some circumstances and not in others. This mechanism is required to allow inherited classes to be extended, yet allow the underlying procedures to coexist with their extended counterparts.

When a procedure is inhertited from another class it can be overridden by a virtual procedure. This allows the a call to the procedure to invoke either the inherited procedure or the virtual procedure depending on the context. This is best demonstrated with an example.


When Do.It is called and type of the the first argument is Minor, the version in the Major class is invoked. If the first argument has the type Minor the version in the Minor class is called. Other procedures with a parameter whose type is Minor can receive arguments that are either Major or Minor types since Major inherits Minor.


The To.It procedure is declared Virtual in the Major class. When To.It is called and the type of the first argument is Major the version in the Major class is called, as is the case with the non-virtual procedure, Do.It. However, if the To.It procedure is called and the first argument is Minor, either version may be called depending on the context. In In the default context, the version in the Minor class is called.


The context changes when any procedure whose first parameter has the type Minor is called and the first argument passed in is Major. The context reverts back to the previous context after the call. Once the context has changed, if the To.It procedure is invoked with the Minor type, the version in the Major class is invoked instead.


Presumably, the variable passed to the virtual procedure is actually the same argument passed to the procedure that caused the context to change. If it was instead actually another variable with a Minor type, there would be problems. To help avoid this, virtual procedures are generic and only the first parameter in a procedure may be passed to a virtual procedure. Still, virtual procedures must be carefully applied. This can only cause problems in cases where an inherited class is extended with additional data structure fields.


Class Body

Class Map File