What Is a Class

Object Pascal supports classes

A class is a container that holds the following members

  1. Fields: Variables inside a class
  2. Methods: Procedures / functions within a class
  3. Properties: Syntactically similar to fields, yet implemented by a pair of read and write methods under the hood

Classes can also contain other types of members, which will be covered later.

Classes are a set of syntax rules designed for object-oriented programming. Their purpose is to encapsulate variables and functions together. Variables describe inherent characteristics of a category of objects, while functions represent the behaviors and actions of those objects.

In the real world, when we describe an entity by stating what it is and what it can do: “what it is” corresponds to properties, and “what it can do” corresponds to behaviors. For example, humans have properties such as name, age and gender, as well as actions including walking, eating and singing.

type
  TMyClass = class
    MyInt: Integer; // Field
    property MyIntProperty: Integer read MyInt write MyInt; // Property
    procedure MyMethod; // Method
  end;

procedure TMyClass.MyMethod;
begin
  WriteLn(MyInt + 10);
end;

The snippet above demonstrates class declaration syntax; this code cannot be executed yet. We will elaborate on execution in subsequent sections.

What Is a Class

Leave a Reply

Your email address will not be published. Required fields are marked *