Observing carefully, one would notice that objects in Pascal exhibit some characteristics of object-oriented programming, such as encapsulation, data hiding, and data inheritance. However, objects in Pascal do not engage in polymorphism. Therefore, classes in Pascal are utilized to implement object-oriented behaviors correctly in a program, particularly in GUI-based software.
Classes in Pascal are defined quite similarly to objects in Pascal, with the distinction that classes in Pascal act as pointers to objects. From a technical perspective, classes are allocated on the Heap of the program, while objects are allocated on the Stack.
In other words, when declaring an object type variable, it occupies as much space on the Stack as the size of the object, but when declaring a class type variable, it takes the size of a pointer on the Stack. The actual class data will be allocated on the Heap.
Defining classes in Pascal
Classes in Pascal are declared similarly to object declarations, using type declarations. The general structure of a class declaration is as follows:
Some notes to remember:
The constructor is defined beforehand in the Root class named Create. Every abstract class and concrete class is a subclass of Root, so all classes have at least one constructor.
Additionally, in the Root class, there is a predefined destructor named Destroy. Every abstract class and concrete class is a subclass of Root, so all classes have at least one destructor.
The following example defines a Rectangle class with 2 integer data members: length and width, and some member functions to manipulate the data members and a procedure to draw a rectangle.
The next example writes a complete program to create an object of the Rectangle class and draw a rectangle. This example is similar to the one mentioned by Mytour in the object section in Pascal. The two programs are alike, differing only in the program below:
- You must add {$mode objfpc} to utilize classes.
- Add {$m+} to utilize constructors.
- An instance of a class differs from an instance of an object. Merely declaring a variable does not allocate space for an instance, you will need to use the create constructor to allocate memory.
Below is a complete example:
Display Attributes of Member Classes
Display attributes refer to the accessibility of members, i.e., the exact position at which members can be accessed. Classes in Pascal have 5 display levels: Public, Private, Strict Private, Protected, and Published.
Constructor and Destructor for Classes in Pascal
Constructor is a special method, called automatically whenever an object is created. Thus users can leverage these behaviors by initializing many things through the constructor function.
Pascal provides a special function called create() to define the constructor. You can add multiple parameters to the constructor function if desired.
The following example creates a constructor for a class named Books and it will initialize the price and name for the book at the time of object creation.
When the above code is compiled and executed, it will return the following result:
The destructor method destroys the object by releasing all resources used within the class.
Inheritance in Pascal Classes
Pascal definitions may optionally inherit from a parent class definition, using the following syntax:
type
childClass-identifier = class(baseClass-identifier)
< members=''>
end;
The example below provides the Novels class, inheriting from the Books class and adding some functions based on requirements.
When the above code is compiled and executed, it will return the following result:
Some important notes:
- Members of the Books class must have the Protected display attribute.
- The Novels class has 2 constructors, so operator overloading is used for overloaded functions.
- The Books.Display procedure is declared virtual, so methods from the Novels class can override it.
- Constructor Novels.Create calls the class constructor using the inherited keyword.
Interface in Pascal
Interfaces in Pascal are defined to provide common function names for implementers. Implementers can implement interfaces as needed. Interfaces can be thought of as skeletons, implemented by developers. Below is an example of an interface in Pascal:
Note, when a class implements an interface, that class must implement all methods of the interface. If a method of the interface is not implemented, the compiler will return an error message.
Abstract Class in Pascal
An abstract class in Pascal is a class that cannot be instantiated, only inherited. Abstract classes are defined by adding the abstract keyword in the class definition as shown below:
When inheriting from an abstract class, all abstract methods in the class declaration of the parent class must be defined by the subclass, and furthermore, this method must be defined with the same display attribute.
Static Keyword in Pascal
When declaring member classes or methods as static, they can be accessed without needing to instantiate the class. A member declared as static cannot be accessed with an object instance of the class.
Refer to the example below to understand this concept better:
When the above code is compiled and executed, it will return the following result:
You must use {$static on} for static members.
In this article, readers have explored the concept of classes in Pascal with Mytour. Readers can delve into many other Pascal articles on Mytour such as arrays in Pascal to gain a better understanding of this language. If you have any questions or queries, feel free to leave your thoughts and evaluations in the comment section below the article.
