Advertisement
Guest User

Untitled

a guest
Sep 8th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. MODULE PointerBirds;
  2.    IMPORT Out;
  3.  
  4.    TYPE BirdRec*   = RECORD sound* : ARRAY 10 OF CHAR; END;
  5.         DuckRec*   = RECORD(BirdRec) END;
  6.         CuckooRec* = RECORD(BirdRec) END;
  7.  
  8.    TYPE Bird   = POINTER TO BirdRec;
  9.         Cuckoo = POINTER TO CuckooRec;
  10.         Duck   = POINTER TO DuckRec;
  11.  
  12.     VAR pb : Bird;
  13.         pc : Cuckoo;
  14.         pd : Duck;
  15.  
  16.     PROCEDURE makeDuckSound*( bird : Duck );
  17.     BEGIN COPY("Quack!", bird.sound) END makeDuckSound;
  18.    
  19.     PROCEDURE makeCuckooSound*( bird : Cuckoo );
  20.     BEGIN COPY("Cuckoo!", bird.sound) END makeCuckooSound;
  21.  
  22.     PROCEDURE makeSound*( bird : Bird );
  23.     BEGIN
  24.         WITH bird : Cuckoo DO makeCuckooSound(bird);
  25.            | bird : Duck   DO makeDuckSound(bird)
  26.           ELSE
  27.             COPY("Tweet!", bird.sound);
  28.         END;
  29.     END makeSound;
  30.  
  31. BEGIN
  32.    NEW(pc);
  33.    NEW(pd);
  34.    
  35.    makeCuckooSound( pc );
  36.    makeDuckSound( pd );
  37.    
  38.    Out.Ln;Out.String( pc^.sound );Out.Ln;
  39.    Out.Ln;Out.String( pd^.sound );Out.Ln;
  40.  
  41.    makeSound( pc );
  42.    makeSound( pd );
  43.  
  44.    Out.Ln;Out.String( pc^.sound );Out.Ln;
  45.    Out.Ln;Out.String( pd^.sound );Out.Ln;
  46. (* -------------------------------------- *)
  47. (* Pass dynamic type to procedure         *)
  48.  
  49.    pb := pd;
  50.  
  51.    makeDuckSound( pb(Duck) );
  52.    Out.Ln;Out.String( pb^.sound );Out.Ln;
  53.  
  54.    pb := pc;
  55.  
  56.    makeCuckooSound( pb(Cuckoo) );
  57.    Out.Ln;Out.String( pb^.sound );Out.Ln;
  58. (* -------------------------------------- *)
  59.  
  60.    makeSound(pb);
  61.    Out.Ln;Out.String( pb^.sound );Out.Ln;
  62.  
  63.    pb := pd;
  64.    
  65.    makeSound(pb);
  66.    Out.Ln;Out.String( pb^.sound );Out.Ln;
  67. (* -------------------------------------- *)
  68.    NEW(pb);
  69.    
  70.    makeSound(pb);
  71.    Out.Ln;Out.String( pb^.sound );Out.Ln;
  72.    
  73. END PointerBirds.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement