Jump to content

Pascal Programming/Object oriented

From Wikibooks, open books for an open world

Back to Pascal Programming

Object Oriented Pascal allows the user to create applications with Classes and Types. This saves the developer time on developing programs that would be very flexible.

This is a sample program (tested with the FreePascal compiler) that will store a number 1 in private variable One, increase it by one and then print it.

 program types;  // this is a simple program
 type MyType=class
       private
        One:Integer;
       public
        function Myget():integer;
        procedure Myset(val:integer);
        procedure Increase();
      end;

 function MyType.Myget():integer;
 begin
   Myget:=One;
 end;
 procedure MyType.Myset(val:integer);
 begin
   One:=val;
 end;
 procedure MyType.Increase();
 begin
   One:=One+1;
 end;

 var
   NumberClass:MyType;
 begin
   NumberClass:=MyType.Create;  // creating instance
   NumberClass.Myset(1);
   NumberClass.Increase();
   writeln('Result: ',NumberClass.Myget());
   NumberClass.Free;  // destroy instance
   NumberClass := Nil;
 end.

This example is very basic and would be pretty useless when used as OOP. Much more complicated examples can be found in Delphi and Lazarus which include a lot of Object Oriented programming.