Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MODULE PointerBirds;
- IMPORT Out;
- TYPE BirdRec* = RECORD sound* : ARRAY 10 OF CHAR; END;
- DuckRec* = RECORD(BirdRec) END;
- CuckooRec* = RECORD(BirdRec) END;
- TYPE Bird = POINTER TO BirdRec;
- Cuckoo = POINTER TO CuckooRec;
- Duck = POINTER TO DuckRec;
- VAR pb : Bird;
- pc : Cuckoo;
- pd : Duck;
- PROCEDURE makeDuckSound*( bird : Duck );
- BEGIN COPY("Quack!", bird.sound) END makeDuckSound;
- PROCEDURE makeCuckooSound*( bird : Cuckoo );
- BEGIN COPY("Cuckoo!", bird.sound) END makeCuckooSound;
- PROCEDURE makeSound*( bird : Bird );
- BEGIN
- WITH bird : Cuckoo DO makeCuckooSound(bird);
- | bird : Duck DO makeDuckSound(bird)
- ELSE
- COPY("Tweet!", bird.sound);
- END;
- END makeSound;
- BEGIN
- NEW(pc);
- NEW(pd);
- makeCuckooSound( pc );
- makeDuckSound( pd );
- Out.Ln;Out.String( pc^.sound );Out.Ln;
- Out.Ln;Out.String( pd^.sound );Out.Ln;
- makeSound( pc );
- makeSound( pd );
- Out.Ln;Out.String( pc^.sound );Out.Ln;
- Out.Ln;Out.String( pd^.sound );Out.Ln;
- (* -------------------------------------- *)
- (* Pass dynamic type to procedure *)
- pb := pd;
- makeDuckSound( pb(Duck) );
- Out.Ln;Out.String( pb^.sound );Out.Ln;
- pb := pc;
- makeCuckooSound( pb(Cuckoo) );
- Out.Ln;Out.String( pb^.sound );Out.Ln;
- (* -------------------------------------- *)
- makeSound(pb);
- Out.Ln;Out.String( pb^.sound );Out.Ln;
- pb := pd;
- makeSound(pb);
- Out.Ln;Out.String( pb^.sound );Out.Ln;
- (* -------------------------------------- *)
- NEW(pb);
- makeSound(pb);
- Out.Ln;Out.String( pb^.sound );Out.Ln;
- END PointerBirds.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement