Introduction to objective Pascal classes

Pascal has two implementations for OOP, the object and the class. There are notable differences between the two.

An object is similar to a record in Pascal, except that it has methods. Methods can be procedures or functions. An object supports almost all the paradigms of OOP except for polymorphism. Moreover, an object is allocated on the stack.

In contrast, class supports polymorphism and is allocated on the heap. A variable of type class is actually a pointer to the class object that has been allocated on the heap.

Defining a class

A class is defined similarly to an object in Pascal:

Type
    ExampleClass = class
        private:
            var1: type; var2: type;...;varn:type;
        public
            constructor create();
            procedure proc1;  
            function f1(): function-type;
end;
var
myClass : ExampleClass;

A class is defined using the class keyword, and the class definitions should always be under the Type declaration portion of the program. Methods are declared inside the class; however, they are implemented elsewhere in the code.

By default, classes come with a constructor Create and a destructor Destroy. These methods are inherited from the Root class that is the ancestor of every class in the program.

Visibility

Pascal classes can either have public, private, or protected members. All members are public by default.

  • Public members are visible to any unit outside the scope of the object.

  • Private members are visible only within the module where the object has been declared.

  • Protected members are only visible to objects that inherit from the base object, even if they are in different modules.

Type
Vector2D = Object
x, y : integer;
function Length: real;
end;
function Vector2D.Length(): real;
begin
Length := Sqrt(x*x + y*y);
end;
var
myVector : Vector2D;
begin
myVector.x := 5;
myVector.y := 3;
writeln(myVector.Length());
end.

In the above code, we create an object called myVector. myVector stores the x-coordinate and the y-coordinate in the member variables x and y, and a member function Length. Since all member variables and functions are public by default, we can access them from outside the scope of the object itself, as seen above. We can access and set the member variables from the main code block.

However, if we were to have:

Type
    Vector2D = Object
        private
           x, y : integer;
        public
           function Length: real;
    end;

This would still be visible outside the object but not outside the module. In other words, if we replaced the example code with the above snippet, the code would still work because it is in the same module.

Similarly, if we were to have protected members, they would only be visible in the descendent objects.

Inheritance

Pascal classes can inherit from other objects as well. The derived classes inherit all the members of the parent object.

Let’s add a class Vector3D and a class Vector2D to see inheritance in action in Pascal.

program Hello;
Type
Vector2D = Object
x, y : integer;
function Length: real;
procedure setX(val: integer);
procedure setY(val: integer);
end;
Vector3D = Object(Vector2D)
z : integer;
function Length: real;
procedure setZ(val: integer);
end;
function Vector2D.Length(): real;
begin
Length := Sqrt(x*x + y*y);
end;
procedure Vector2D.setX(val : integer);
begin
x := val;
end;
procedure Vector2D.setY(val : integer);
begin
y := val;
end;
function Vector3D.Length(): real;
begin
Length := Sqrt(x*x + y*y + z*z);
end;
procedure Vector3D.setZ(val : integer);
begin
z := val;
end;
Var
myVector : Vector2D;
myVector3D : Vector3D;
temp : real;
begin
myVector.x := 5;
myVector.y := 3;
myVector3D.x := 9;
myVector3D.setY(10);
temp := myVector3D.Length();
writeln(myVector3D.x, ' ', myVector3D.y, ' length =', temp);
end.

The class Vector2D defines two coordinates, x and y. The derived class that inherits from Vector2D is called Vector3D. As you can see above, it only defines one other variable, z, pertaining to the z-coordinate. The variables x and y are inherited from the parent class and can be used in methods defined inside Vector3D.

Abstract classes and polymorphism

Polymorphism is a feature of object-oriented programming that allows different classes to have the same interface for representing different data and operations.

Abstract classes are only inherited from other classes and cannot be explicitly instantiated.

Abstract classes are created the same way regular classes are defined. However, using the abstract keyword before the class identifier makes it an abstract class. A class in Pascal can be declared abstract even if it does not contain any virtual methods.

Type
    className = abstract class (ParentClass)
        var1: type, var2: type, ...
        procedure method_1; abstract;
        function method_2(); return-type;
end;
Copyright ©2024 Educative, Inc. All rights reserved