A Class type in Pascal refers to a structure that contains fields, methods, and other properties to represent an entity.
Class types are similar to Object types, except that a class is a pointer to an object, i.e., an object can be considered an instance of a class.
The structure of a class is illustrated below:
Classes in Pascal contain the following:
Fields: These refer to variables that represent data items to be stored in the class.
Methods: These refer to procedures or functions that will operate on a class instance.
Visibility Keywords: These specify the scope of the fields and methods as either public
, private
, or protected
.
Constructor: A special method automatically invoked when an instance of a class is created. Every class has a predefined constructor called Create
.
Destructor: A special method automatically invoked when an instance of the class is destroyed or goes out of scope. Every class has a predefined destructor called Destroy
.
To declare a class in Pascal, you will need to include a special directive to define classes, as shown below:
{$mode objfpc}
Similarly, you will need to include another special directive to use constructors, as shown below:
{$m+}
Classes are declared through the type
declaration, as shown below:
type class_name = class
private
field1 : data_type;
field2: data_Type;
//... as many fields as needed
public:
constructor c();
procedure method1;
function method2(): data_type;
//... as many methods as needed
end;
As shown above, all member variables and methods are encapsulated by the type
declaration.
The code below shows how a class can be declared in Pascal:
// Directives to use classes{$mode objfpc}{$m+}program exampleClass;// Declaring a person Classtype Person = class// fields to hold data about a personprivateperson_name : string;age : integer;public// constructor for intializing a class instanceconstructor c(n : string; a : integer);// methods to view and update person dataprocedure setName(n : string);function getName() : string;procedure setAge(a : integer);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', 20);//output fieldswriteln('The person''s name is ', newPerson.getName());writeln('The person''s age is ', newPerson.getAge());//updating fieldsnewPerson.setName('Bob');newPerson.setAge(30);//output fieldswriteln('The person''s name is ', newPerson.getName());writeln('The person''s age is ', newPerson.getAge());end.
The above code declares a Person
class using the type
identifier. The class has fields for the person’s name and age. These fields can only be accessed within the module that contains the class due to the private
keyword.
The class declaration includes a constructor c
. The constructor is necessary as an instance of the class cannot be created just by declaring a variable; instead, the constructor must be used to allocate memory for the instance.
The declaration also includes procedures to change the person’s name and age and functions to retrieve the values of these fields. Each of these procedures and functions is defined after the class declaration.
Finally, the code creates an instance of the Person
class called newPerson
. The constructor is used to initialize the instance with the specified values for the fields.
The setName
and setAge
methods update the person’s name and age. The updated name and age are retrieved using the getName
and getAge
methods and output accordingly.
Free Resources