What are object constructors and destructors in Pascal?

Constructors and destructors are special methods defined inside an object that are automatically invoked when an object is created or destroyed, respectively.

The process is illustrated below:

Constructors

Constructors are automatically invoked when an object is created. The prototype of a constructor is shown below:

constructor Object.constructor_name(argument(s): type1; argument(s): type2; ...);

//local variables
begin
   //constructor body
end;

Normally, the constructor is named init; however, you can give it any name you want.

Destructors

Destructors are invoked when an object is destroyed or goes out of scope. The prototype of a destructor is shown below:

destructor Object.destructor_name(argument(s): type1; argument(s): type2; ...);

//local variables
begin
   //destructor body
end;

Destructors free any memory allocated to the object.

Note: The keyword Object in the constructor and destructor prototypes refers to the object’s name. Both constructors and destructors accept any number of arguments.

Example

The code below shows how constructors and destructors can be used in Pascal:


program objMethods;
// Declaring a person Object
type Person = object
// fields to hold data about a person
private
person_name : string;
age : integer;
// object methods
public
// constructor for object instantiation
constructor createPerson(n : string; a : integer);
// procedures to update data fields
procedure setName(n : string);
procedure setAge(a : integer);
// functions to return data field values
function getName() : string;
function getAge() : integer;
// destructor for when object is destroyed
destructor destroyPerson;
end;
// Defining the object methods
constructor Person.createPerson(n : string; a : integer);
begin
person_name := n;
age := a;
end;
destructor Person.destroyPerson;
begin
writeln('Destroying the Person object');
end;
procedure Person.setName(n : string);
begin
person_name := n;
end;
function Person.getName() : string;
begin
getName := person_name;
end;
procedure Person.setAge(a : integer);
begin
age := a;
end;
function Person.getAge() : integer;
begin
getAge := age;
end;
// creating object instance
var
newPerson : Person;
begin
// initializing object
newPerson.createPerson('Alice', 30);
//output object fields
writeln('The person''s name is ', newPerson.getName());
writeln('The person''s age is ', newPerson.getAge());
//setting object fields
newPerson.setName('Bob');
newPerson.setAge(20);
//output object fields
writeln('The person''s name is ', newPerson.getName());
writeln('The person''s age is ', newPerson.getAge());
// Destroy object
newPerson.destroyPerson;
end.

Explanation

The Person object holds data about a person’s name and age.

The code defines a constructor, createPerson, that can be invoked to initialize the object with specific values and a destructor, destroyPerson, for when the object is destroyed.

setName and setAge can update a person’s name and age, respectively. Similarly, the getName and getAge methods return the person’s name and age, respectively.

Since all these methods are classified under the public keyword, they are always accessible.

Once the object newPerson is created, the constructor, createPerson, is invoked to initialize the values for the name and age to “Alice” and 3030, respectively. The getName and getAge methods return these values, which are output accordingly.

The setName and setAge methods are invoked to change the values for name and age to “Bob” and 2020, respectively. The updated values are retrieved and output.

Finally, the destructor, destroyPerson, is called to destroy the object before the program ends. The destructor outputs a message and frees any memory allocated to the object.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved