Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

pascal

How to invoke object methods in Pascal

Talha Ashar

Object methods in Pascal are methods defined inside an object that can access the object’s data fields. An object method can be invoked by using the dot operator ( . ) on an object instance.

The process is illustrated below:

Example

The code below shows how object methods can be invoked in Pascal:

program invokeMethods;

// 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;
        
end;

// Defining the object methods
constructor Person.init(n : string; a : integer);
begin
    person_name := n;
    age := a;
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());
  
end.

Explanation

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. 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 through the dot operator ( . ) to initialize the values for the name and age to “Alice” and 3030, respectively. The getName and getAge methods are also invoked through the dot operator to return the field values, which are output accordingly. Since the object declaration does not list any parameters for the getName and getAge functions, no arguments are provided when the method is invoked.

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

RELATED TAGS

pascal

CONTRIBUTOR

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

View all Courses

Keep Exploring