What are class methods in Pascal?
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:
Types of methods
A class 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 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 values of 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 instance of the class is created. The prototype of a constructor is shown below:
constructor Class.constructor_name(argument(s): type1; argument(s): type2; ...);
//local variables
begin
//constructor body
end;
- Destructor: A special method automatically invoked when a class instance is destroyed or goes out of scope. The prototype of a destructor is shown below:
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
Classin the constructor and destructor prototypes refers to the name of the class.
Example
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.
Explanation
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