Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

pascal
object oriented

What are Pascal objects?

Muhammad Ashir

Pascal supports two Object-Oriented ProgrammingOOP implementations, objects and classes. In this article, we will be talking about objects in Pascal.

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.

Defining an object

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;

Type
    Vector2D = Object
        x, y : integer;
        function Length: real;
        procedure setX(val: integer);
        procedure setY(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;


Var
  myVector : Vector2D;
  temp : real;

begin
    myVector.x := 5;
    myVector.y := 3;
    temp := myVector.Length();
    writeln ('temp = ', temp);
end.

Visibility of Pascal objects

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 and destructors

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;

Type
    Vector2D = Object
        x, y : integer;
        constructor init;
        destructor done;
        function Length: real;
        procedure setX(val: integer);
        procedure setY(val: integer);
    end;
    
constructor Vector2D.init;
begin
    writeln('Vector created')
end;

destructor Vector2D.done;
begin
    writeln('Vector destroyed')
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;


Var
  myVector : Vector2D;
  temp : real;

begin
    myVector.init();
    myVector.x := 5;
    myVector.y := 3;
    myVector.done();
    temp := myVector.Length();
    writeln ('temp = ', temp);
    myVector.done();

end.

Inheritance

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;

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 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.

RELATED TAGS

pascal
object oriented

CONTRIBUTOR

Muhammad Ashir
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring