Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

pascal

What are object methods in Pascal?

Talha Ashar

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:

Types of methods

An object can contain the following types of methods:

  • Procedures: These methods are subprograms that do not directly return a value. They are typically used when changing the value of an object’s data fields. The prototype of a procedure is shown below:
procedure procedure_name(argument(s): type1; argument(s): type2; ...);

//local variables
begin
   //procedure body
end;
  • Functions: These methods are subprograms that return a value. They are typically used to return the value of the object’s data fields. The prototype of a function is shown below:
function function_name(argument(s): type1; argument(s): type2; ...): function_type;

//local variables
begin
   //function body
end;
  • Constructor: A special method that is 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;
  • Destructor: A special method automatically 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;

All object methods accept any number of parameters.

Note: The keyword Object in the constructor and destructor prototypes refers to the object’s name.

Example

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.

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 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 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 the name and age to “Bob” and 2020, respectively. The updated values are retrieved and output.

Finally, destructor is called to destroy the object before the program ends.

RELATED TAGS

pascal

CONTRIBUTOR

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

View all Courses

Keep Exploring