Object methods in Pascal are methods defined inside an object that can access the object’s data fields.
The illustration below shows the types of methods available to an object:
An object can contain the following types of methods:
procedure procedure_name(argument(s): type1; argument(s): type2; ...);
//local variables
begin
//procedure body
end;
function
is shown below:function function_name(argument(s): type1; argument(s): type2; ...): function_type;
//local variables
begin
//function body
end;
constructor
is shown below:constructor Object.constructor_name(argument(s): type1; argument(s): type2; ...);
//local variables
begin
//constructor body
end;
destructor
is shown below:destructor Object.destructor_name(argument(s): type1; argument(s): type2; ...);
//local variables
begin
//destructor body
end;
All object methods accept any number of parameters.
Note: The keyword
Object
in theconstructor
anddestructor
prototypes refers to the object’s name.
The code below shows how object methods 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 init(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 done; end; // Defining the object methods constructor Person.init(n : string; a : integer); begin person_name := n; age := a; end; destructor Person.done; begin writeln('Destroying the 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.init('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.done; end.
The Person
object holds data about a person’s name and age.
The code defines a constructor
that can be invoked to initialize the object with specific values and a destructor
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
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 the name and age to “Bob” and , respectively. The updated values are retrieved and output.
Finally, destructor
is called to destroy the object before the program ends.
RELATED TAGS
CONTRIBUTOR
View all Courses