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 Class in 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 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.

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 3030, 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 2020, respectively. The updated values are retrieved and output.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved