Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

pascal

How to declare a class type in Pascal

Talha Ashar

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:

Structure of classes

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.

Declaration

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.

Example

The code below shows how a class can be declared in Pascal:

// Directives to use classes
{$mode objfpc}
{$m+}

program exampleClass;

// Declaring a person Class
type Person = class
    // fields to hold data about a person
    private
        person_name : string;
        age : integer;
    
    public
        // constructor for intializing a class instance
        constructor c(n : string; a : integer);
        
        // methods to view and update person data
        procedure setName(n : string);
        function getName() : string;
        
        procedure setAge(a : integer);
        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', 20);
  
  //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(30);
  
  //output fields  
  writeln('The person''s name is ', newPerson.getName());
  writeln('The person''s age is ', newPerson.getAge());
end.

Explanation

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.

RELATED TAGS

pascal

CONTRIBUTOR

Talha Ashar
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring