Pascal supports two
Objects are a user-defined data type that is similar to records. However, unlike records, objects also contain functions and procedures. The procedures and functions in an object are defined in the method field.
Objects support all of the paradigms of OOP except polymorphism. This includes inheritance, variable visibility, constructors, and destructors. Objects are allocated on the stack.
Pascal classes, on the other hand, implement the polymorphism features of OOP as well. Classes are allocated on the heap instead of the stack.
Pascal objects are defined in almost the same way as records. They are defined under the type declaration as follows:
Type
Vector2D = Object
x, y : integer;
function Length: real;
procedure setX(val: integer);
procedure setY(val: integer);
end;
Here, we declare an object called Vector2D
that represents a 2D vector. It has two member variables, x
and y
, and three methods, Length()
, setX()
, and setY()
.
The procedures and functions are defined later, as follows:
function Vector2D.Length(): real;
begin
Length := Sqrt(x*x + y*y);
end;
procedure Vector2D.setX(val : integer);
begin
x := val;
end;
Here is the complete code that shows how to use an object in Pascal.
program Hello;TypeVector2D = Objectx, y : integer;function Length: real;procedure setX(val: integer);procedure setY(val: integer);end;function Vector2D.Length(): real;beginLength := Sqrt(x*x + y*y);end;procedure Vector2D.setX(val : integer);beginx := val;end;procedure Vector2D.setY(val : integer);beginy := val;end;VarmyVector : Vector2D;temp : real;beginmyVector.x := 5;myVector.y := 3;temp := myVector.Length();writeln ('temp = ', temp);end.
Pascal objects 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 scope of the object.
Protected members are only visible to objects that inherit from the base object.
Constructors are special functions in OOP that are called only when the object is created. To define a constructor in Pascal, all we need to do is use the keyword constructor
. Conventionally, the constructor is named init
, but you can name it whatever you want.
The destructor is a special function that is called when the object is destroyed. In Pascal, you can define a destructor using the keyword destructor
. The destructor should deallocate any memory used by the object. In Pascal, you can call constructors and destructors more than once.
program Hello;TypeVector2D = Objectx, y : integer;constructor init;destructor done;function Length: real;procedure setX(val: integer);procedure setY(val: integer);end;constructor Vector2D.init;beginwriteln('Vector created')end;destructor Vector2D.done;beginwriteln('Vector destroyed')end;function Vector2D.Length(): real;beginLength := Sqrt(x*x + y*y);end;procedure Vector2D.setX(val : integer);beginx := val;end;procedure Vector2D.setY(val : integer);beginy := val;end;VarmyVector : Vector2D;temp : real;beginmyVector.init();myVector.x := 5;myVector.y := 3;myVector.done();temp := myVector.Length();writeln ('temp = ', temp);myVector.done();end.
Pascal objects can inherit from other objects as well. The derived objects inherit all the members of the parent object.
Let’s add a class Vector3D
to our previous example and see how inheritance works in Pascal.
program Hello;TypeVector2D = Objectx, 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;beginLength := Sqrt(x*x + y*y);end;procedure Vector2D.setX(val : integer);beginx := val;end;procedure Vector2D.setY(val : integer);beginy := val;end;function Vector3D.Length(): real;beginLength := Sqrt(x*x + y*y + z*z);end;procedure Vector3D.setZ(val : integer);beginz := val;end;VarmyVector : Vector2D;myVector3D : Vector3D;temp : real;beginmyVector.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 Vectror3D
. 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
.
Free Resources