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 Class type Person = class // fields to hold data about a person private person_name : string; age : integer; // class methods public // constructor for intializing a class instance constructor c(n : string; a : integer); // methods to change data fields procedure setName(n : string); procedure setAge(a : integer); // methods to return values of data fields function getName() : string; function getAge() : integer; end; // Defining the class methods constructor Person.c(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; var newPerson : Person; begin // creating class instance newPerson := Person.c('Alice', 30); //output fields writeln('The person''s name is ', newPerson.getName()); writeln('The person''s age is ', newPerson.getAge()); //updating fields newPerson.setName('Bob'); newPerson.setAge(20); //output fields writeln('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.
RELATED TAGS
CONTRIBUTOR
View all Courses