Object-Oriented Pascal

In this tutorial we show how you can use classes and objects in console programs. This will prepare you for visual programming, which is object-oriented. You should be able to handle records confidently before studying this section.

A record makes it easy to refer to a collection of fields of different types. An object extends the functionality of a record. It has a collection of data items and also contains procedures and functions that operate on its data. These procedures and functions are called methods. An object should be an instance of a class, so that you can use one class definition for as many objects as you need of that type. Usefully, one (derived) class can inherit from another (base) class. The derived class can use fields and methods in the base class plus additional fields and methods of its own. The Inheritance section provides examples.

It is useful to have private fields (which you cannot access directly from another unit) or protected fields (which you can access directly only from the same unit or from derived classes). Usually, you must access private data using public methods. In this way, you hide (encapsulate) data and can ensure its validity. You should be able to change the implementation of methods within the 'black box' of the class without rewriting code that uses the class. The Properties section shows how you can use special accessor fields to execute methods contained within your class.

The tutorial ends with a section on the use of some of the many inbuilt classes by console programs. Some of the demonstration programs display in the usual console window and another draws graphics for output to form, file and printer. We provide another demonstration of object-oriented Pascal in the tutorial Lists, Stacks and Queues and the Game Development tutorial is a rich source of examples.

Since we wrote this tutorial several students have become fluent with object-oriented Pascal and have contributed instructive programs. See, for example, Christopher Winward's AStar and Steven Binns's Invader.

Follow these links to the main sections of this tutorial.

Programming - a skill for life!

Pascal Programming Tutorials