Class methods in Pascal are methods defined inside a class that can access the class’s data fields.
The illustration below shows the types of methods available to a class:
A class can contain the following types of methods:
procedure procedure_name(argument(s): type1; argument(s): type2; ...);
//local variables
begin
//procedure body
end;
function function_name(argument(s): type1; argument(s): type2; ...): function_type;
//local variables
begin
//function body
end;
constructor Class.constructor_name(argument(s): type1; argument(s): type2; ...);
//local variables
begin
//constructor body
end;
destructor Class.destructor_name(argument(s): type1; argument(s): type2; ...);
//local variables
begin
//destructor body
end;
All class methods accept any number of parameters.
Note: The keyword
Class
in the constructor and destructor prototypes refers to the name of the class.
The code below shows how class methods can be used in Pascal:
// Directives to use classes{$mode objfpc}{$m+}program classMethods;// Declaring a person Classtype Person = class// fields to hold data about a personprivateperson_name : string;age : integer;// class methodspublic// constructor for intializing a class instanceconstructor c(n : string; a : integer);// methods to change data fieldsprocedure setName(n : string);procedure setAge(a : integer);// methods to return values of data fieldsfunction getName() : string;function getAge() : integer;end;// Defining the class methodsconstructor Person.c(n : string; a : integer);beginperson_name := n;age := a;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;varnewPerson : Person;begin// creating class instancenewPerson := Person.c('Alice', 30);//output fieldswriteln('The person''s name is ', newPerson.getName());writeln('The person''s age is ', newPerson.getAge());//updating fieldsnewPerson.setName('Bob');newPerson.setAge(20);//output fieldswriteln('The person''s name is ', newPerson.getName());writeln('The person''s age is ', newPerson.getAge());end.
The Person
class holds data about a person’s name and age.
The code defines a constructor
that can be invoked to initialize a class instance 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 instance 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.
Finally, 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.
Free Resources