Day 2

This is the day 2 page for object oriented programming course 102, an introduction to object oriented programming. On day 2 we introduce the explore a few of the popular of classes and objects in Free Pascal class library and compare them to classes adn objects in other languages.

Below are the topics and course materials we are learning today.

Examples of objects in a few languages

Most if not all object orient programming languages include a base class library that defines a common set of classes to handle some of the basic chores you'll need to address when writing programs. In the sections below we'll look at some of the classes that are included with Free Pascal and also Lazarus. We'll compare these classes to their equivalents in other languages, notably the C# and Java languages.

TObject

TObject
Free Pascal, C#, Java and several other languages follow an object oriented design structure where every class type ultimately inherits from a single base class. In Free Pascal every object and class descends from TObject which is defined in the built in System unit. In C# the ultimate base class for all objects is System.Object. In Java the single class from with all other derive is java.lang.Object.

Much of the design and functionality of the base object across these languages are inspired by a language called Delphi, of which Free Pascal is an open source reimplementation. Anders Hejlsberg, the original designer of Delphi, was hired away by Microsoft in the mid 1990s to come work for Mirosoft and design their Visual J++ (Java) language and then to design the first version of Microsoft .NET and C#.

TObject defines much of the functionality needed to query objects to determine exactly what they are. This functionality includes methods such as ClassType, ClassName, ClassNameIs, InheritsFrom, Dispatch, GetInterface, and ToString.

As all objects derive from TObject, this means all objects can provide their type, name, validate inheritance, dispatch commands, query for functionality, and be converted to a string. TObject provides other base functionality as well, and since all object derive from TObject they have access to that functionality too.

Exception

TObject Exception
Free Pascal provides an object orient approach to handling errors which might be raised while you program runs. The base class for all errors is the Exception class in the SysUtils unit. We will cover how to work with exceptions in a future lesson, but you may want to refer to this chart to see many of the exception types defined in Free Pascal.

The C# equivalent of the exception class, which is the base class for all error in C#, is defined as System.Exception.

TPersistent

TObject TPersistent
Derived directly from TObject is an important class known as TPersistent class. TPersistent is defined in the useful Classes unit which is typically included in most program written with Free Pascal.

TPersistent defined the functionality and methods save and load objects. It also provides a mechanism by which any persistent object can be copied to another TPersistent, even if they aren't directly related. In C# TPersistent is roughly analogous to System.MarshalByRef, but the Free Pascal name is a bit easier to remember.

TComponent

TObject TPersistent TComponent
Derived directly from TPersistent is yet another an important class known as TComponent. The TComponent class defines ownership of a collection of components, such that a component can be composed of and manage many other components to make extremely useful objects. For example the forms you've created in the previous introduction course are all derived from TComponent, and as such they are able to contain many other components an controls.

Aside from providing the functionality to contain other components, the TComponent class allows to to provide a unique friendly name for multiple components and the ability to find those components using said name.

TControl

TObject TPersistent TComponent TControl
Derived directly from TComponent is the TControl class. The TControl class lays the foundation for creating controls with a visible appearance. Controls have boundaries, can be aligned, anchored, enabled or disabled, and have a z order among other things.

TCollection

TObject TPersistent TCollection
Derived directly from TPersistent is the TCollection class. The TCollection class provides a general container for similar items. Because TCollection is a persistent class the items in a collection can be, saved, loaded, and copied between other collections.

TStrings

TObject TPersistent TStrings
Derived directly from TPersistent is the TStrings class. The TStrings class provides a container for strings that can be specialized for many different uses. Consider the memo control, it has a Lines property of type TStrings. Likewise a SQL statement might be composed of of a TStrings. Lets look at a few use cases of TStrings.

TStream

TObject TStream
Derived directly from TObject is the TStream class. The TStream class provides a an abstract class for saving and loading data to various sources. Derived classes might use files, memory, the network, or even a database as the underlying storage source.

TGraphic

TObject TPersistent TGraphic
Derived from TPersistent is the TGraphic class. The TGraphic class is an abstract base class for various image formats. Derived classes include metafiles, icons, bitmaps, pngs and more.

TCanvas

TObject TPersistent TCanvas
Derived from TPersistent is the TCanvas class. The TCanvas represents an artists canvas allowing you to paint or draw on a surface. Some controls provide a canvas property to allowing for custom painting. Some graphic classes and the printer class also provide a canvas property.

TDataSource

TObject TPersistent TComponent TDataSource
Derived directly from TComponent is the TDataSource class. The TDataSource class is used to connect persistent objects to data stores. In another source we'll examine data stores and database programming, but a data store is an important tool for accessing data from various sources.

Homework

There is no homework for day 2.

See also