Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

pascal

What are class destructors in Pascal?

Talha Ashar

Class destructors are special methods defined inside a class that are automatically invoked when a class instance is destroyed or goes out of scope. You can invoke a destructor using the dot operator ( . ) on a class instance.

The process is illustrated below:

Prototype

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

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

//local variables
begin
   //destructor body
end;

Destructors free any memory allocated to the class instance.

Note: The keyword Class in the destructor prototype refers to the class’s name. Destructors accept any number of arguments.

Example

The code below shows how you can use class destructors in Pascal:

// Directives to use classes
{$mode objfpc}
{$m+}

program classDestructor;

// Declaring a person Class
type Person = class
    // fields to hold data about a person
    private
        person_name : string;
        age : integer;
    
    // class methods
    public
        // constructor for intializing a class instance
        constructor create(n : string; a : integer);
        
        // methods to return values of data fields
        function getName() : string;
        function getAge() : integer;
        
        // destructor to destroy a class instance
        destructor destroy; override;
end;

// Defining the class methods

constructor Person.create(n : string; a : integer);
begin
    person_name := n;
    age := a;
end;

destructor Person.destroy();
begin
    writeln('Destructor invoked! Destroying the instance.');
    person_name := 'Empty';
    age := 0;
    writeln('Changed name to: ', person_name);
    writeln('Changed age to: ', age);
end;

function Person.getName() : string;
begin
    getName := person_name;
end;

function Person.getAge() : integer;
begin
    getAge := age;
end;

var
    newPerson : Person;

begin
  
  // creating class instance
  newPerson := Person.create('Alice', 30);
  
  //output fields  
  writeln('The person''s name is ', newPerson.getName());
  writeln('The person''s age is ', newPerson.getAge());
  
  // invoking destructor to destroy instance
  newPerson.destroy();
end.

Explanation

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

The code defines a constructor, create, that can be invoked to initialize the class instance with specific values. A destructor, destroy, is also defined for when the instance is to be destroyed. The destructor declaration’s keyword override is necessary, as Pascal classes already have a predefined destructor.

ThegetName 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 class instance newPerson is created, the constructor, create, 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.

Finally, the destructor, destroy, is called to destroy the class instance before the program ends. The destructor outputs a message and frees any memory allocated to the instance. As part of the destructor definition, the values of the person_name and age fields are also changed and output.

RELATED TAGS

pascal

CONTRIBUTOR

Talha Ashar
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring