[fpc-pascal]Overloaded Operators?

Sebastian Schuberth s.schuberth at gmx.net
Thu Oct 5 11:52:55 CEST 2000


filexfer3 at juno.com wrote:

> Someone alluded earlier to overloading operators in FPC...
> could I see source for that?

This is the test program I used to see whether operator overloading work
with objects:

--8<--(BEGIN fractal.pas)--
Program Fractal;

Uses
  CRT;

Const
  cr0 = -2.5;
  cr1 = +1.5;
  ci0 = +1.5;
  ci1 = -1.5;

Type
  Float=Single;
  Complex=Object
    Private

    re,im:Float;

    Public

    Function getRadius:Float;
    Function toString:String;
  End;

Function Complex.getRadius:Float;
Begin
  getRadius:=Sqrt(re*re+im*im);
End;

Function Complex.toString:String;
Var
  reStr,imStr:String;
Begin
  Str(re:0:4,reStr);
  Str(im:0:4,imStr);
  toString:='<complex re='+reStr+', im='+imStr+'>';
End;

Operator +(a,b:Complex)z:Complex;
Begin
  z.re:=a.re+b.re;
  z.im:=a.im+b.im;
End;

Operator *(a,b:Complex)z:Complex;
Begin
  z.re:=a.re*b.re-a.im*b.im;
  z.im:=a.re*b.im+a.im*b.re;
End;

Var
  z,c:Complex;
  crInc,ciInc:Float;
  x,y,Count:LongInt;

Begin
  Asm
    mov eax,13h
    int 10h
  End;

  crInc:=(cr1-cr0)/319;
  ciInc:=(ci1-ci0)/199;

  c.im:=ci0;
  For y:=0 To 199 Do Begin
    c.re:=cr0;
    For x:=0 To 319 Do Begin
      z.re:=0;
      z.im:=0;
      Count:=31;

      Repeat
        z:=z*z+c;
        Inc(Count);
      Until (z.getRadius>=2) Or (Count=255);

      Mem[$A0000+y*320+x]:=Count;
      c.re+=crInc;
    End;
    c.im+=ciInc;
  End;

  ReadKey;

  Asm
    mov eax,03h
    int 10h
  End;
End.
--8<--(END fractal.pas)--

-- 

Sebastian Schuberth




More information about the fpc-pascal mailing list