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 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 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.
The code below shows how constructors and destructors can be used in Pascal:
program objMethods;// Declaring a person Objecttype Person = object// fields to hold data about a personprivateperson_name : string;age : integer;// object methodspublic// constructor for object instantiationconstructor createPerson(n : string; a : integer);// procedures to update data fieldsprocedure setName(n : string);procedure setAge(a : integer);// functions to return data field valuesfunction getName() : string;function getAge() : integer;// destructor for when object is destroyeddestructor destroyPerson;end;// Defining the object methodsconstructor Person.createPerson(n : string; a : integer);beginperson_name := n;age := a;end;destructor Person.destroyPerson;beginwriteln('Destroying the Person object');end;procedure Person.setName(n : string);beginperson_name := n;end;function Person.getName() : string;begingetName := person_name;end;procedure Person.setAge(a : integer);beginage := a;end;function Person.getAge() : integer;begingetAge := age;end;// creating object instancevarnewPerson : Person;begin// initializing objectnewPerson.createPerson('Alice', 30);//output object fieldswriteln('The person''s name is ', newPerson.getName());writeln('The person''s age is ', newPerson.getAge());//setting object fieldsnewPerson.setName('Bob');newPerson.setAge(20);//output object fieldswriteln('The person''s name is ', newPerson.getName());writeln('The person''s age is ', newPerson.getAge());// Destroy objectnewPerson.destroyPerson;end.
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 , 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 , 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