Object Oriented Programming in Free Pascal

Free Pascal supports Object Oriened programming paradigm as well as structured programming, so that it is a multi paradigm language.

In this example we will define TCar as a class (type). This class contains attributes (variables) and methods (procedures and functions).

Before using this class, we need to declare and inistantiate objects (variables of class). After finishing using these objects, we will release them from memory:


program oopCars;

{$mode objfpc}{$H+}

uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes, sysutils
{ you can add units after this };

type

{ TCar type }

TCar = class
private // Accessible inside class methods only
  fModel: string;
  fMaker: string;
  fPrice: Single;
public // Accessible from outside class methods
  procedure SaveInfo(AFileName: string);
  procedure LoadInfo(AFileName: string);
  procedure SetModel(AModel: string);
  procedure SetMaker(AMaker: string);
  procedure SetPrice(APrice: single);
  function GetModel: string;
  function GetMaker: string;
  function GetPrice: single;
  constructor Create(Model, Maker: string; Price: single);
  destructor Destroy; override;
end;

{ TCar implementation}

procedure TCar.SaveInfo(AFileName: string);
var
  F: TextFile;
begin
  AssignFile(F, AFileName);
  Rewrite(F);
  Writeln(F, fMaker);
  Writeln(F, fModel);
  Writeln(F, fPrice);
  CloseFile(F);
end;

procedure TCar.LoadInfo(AFileName: string);
var
  F: TextFile;
begin
  AssignFile(F, AFileName);
  Reset(F);
  ReadLn(F, fMaker);
  Readln(F, fModel);
  ReadLn(F, fPrice);
  CloseFile(F);
end;

procedure TCar.SetModel(AModel: string);
begin
  fModel:= AModel;
end;

procedure TCar.SetMaker(AMaker: string);
begin
  fMaker:= AMaker;
end;

procedure TCar.SetPrice(APrice: single);
begin
  fPrice:= APrice;
end;

function TCar.GetModel: string;
begin
  Result:= fModel;
end;

function TCar.GetMaker: string;
begin
  Result:= fMaker;
end;

function TCar.GetPrice: single;
begin
  Result:= fPrice;
end;

constructor TCar.Create(Model, Maker: string; Price: single);
begin
  inherited Create;
  fModel:= Model;
  fMaker:= Maker;
  fPrice:= Price;
end;

destructor TCar.Destroy;
begin
  inherited Destroy;
end;

{$R *.res}

var
  // Declare car objects
  FirstCar: TCar;
  SecondCar: TCar;
  MyCar: TCar;
  begin
  // Initialize car objects
  FirstCar:= TCar.Create('Hyundai', 'Sonata', 22000);
  SecondCar:= TCar.Create('Sunny', 'Nisan', 18000);
  MyCar:= FirstCar; // MyCar pointed to First car

  // Usage
  FirstCar.SaveInfo('corolla.txt');
  Writeln('First car is: ' + FirstCar.GetMaker + '-' + FirstCar.GetModel);
  Writeln('Second car is: ' + SecondCar.GetMaker + '-' + SecondCar.GetModel);
  Writeln('MyCar car is: ' + MyCar.GetMaker + '-' + MyCar.GetModel);

  FirstCar.SetPrice(21000);
  WriteLn('First car price after modify: ', Format('%0.0f', [FirstCar.GetPrice]));
  WriteLn('Second car price : ', Format('%0.0f', [SecondCar.GetPrice]));
  WriteLn('MyCar price modified by First car: ', Format('%0.0f', [MyCar.GetPrice]));

  // Release objects
  FirstCar.Free;
  SecondCar.Free;
  // No need to free MyCar, it is already freed by FirstCar
  Readln;

end.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s