What are object methods in Pascal?
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
functionis 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
constructoris 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
destructoris 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
Objectin theconstructoranddestructorprototypes refers to the object’s name.
Example
The code below shows how object methods 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 init(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 done;end;// Defining the object methodsconstructor Person.init(n : string; a : integer);beginperson_name := n;age := a;end;destructor Person.done;beginwriteln('Destroying the 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.init('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.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 , 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.
Free Resources