oop - How to implement interface in Ada? -


dont know oop pattern called how can same pattern in ada? example code:

interface vehicle{     string function start(); }  class tractor implements vehicle{     string function start(){         return "tractor starting";     } } class car implements vehicle{     string function start(){         return "car  starting";     } }  class testvehicle{     function testvehicle(vehicle vehicle){         print( vehicle.start() );     } } new testvehicle(new tractor); new testvehicle(new car); 

my failed attempt in ada: how fix properly?

with ada.text_io;  procedure main     package packagevehicle       type vehicle interface;       function start(self : vehicle) return string abstract;    end packagevehicle;     type tractor new packagevehicle.vehicle null record;    overriding -- optional    function start(self : tractor) return string    begin       return "tractor starting!";    end start;    type car new packagevehicle.vehicle null record;    overriding -- optional    function start(self : car) return string    begin       return "car starting!";    end start;      procedure testvehicle(vehicle : packagevehicle.vehicle)    begin       ada.text_io.put_line( "testing vehicle" );       ada.text_io.put_line( start(vehicle) );    end;     tractor0 : tractor;    car0 : car;  begin     ada.text_io.put_line( testvehicle(tractor0) );    ada.text_io.put_line( testvehicle(car0) );  end main; 

compiler says: builder results warning: declaration of "testvehicle" late builder results warning: spec should appear after declaration of "vehicle"

the key thing aware of "all user-defined primitive subprograms of interface type shall abstract subprograms or null procedures." (ref) i.e. can't define subprogram takes interface parameter (yes, know different java.) why you're getting error on testvehicles declaration.

essentially, have define type implements interface(s), work type.

the ada rationale chapter on interfaces discusses in detail.

here's working example based on question--i renamed things , fixed couple errors getting lost amongst error messages seeing :-) note addition of type 'concrete_vehicles' instantiates vehicle interface.

with ada.text_io; use ada.text_io;  procedure interface_test     package package_vehicle        type vehicle interface;        function start(self : vehicle) return string abstract;    end package_vehicle;      type concrete_vehicles abstract new package_vehicle.vehicle null record;      type tractor new concrete_vehicles null record;     overriding -- optional    function start(self : tractor) return string    begin       return "tractor starting!";    end start;     type car new concrete_vehicles null record;    overriding -- optional    function start(self : car) return string    begin       return "car starting!";    end start;      procedure testvehicle(vehicle : concrete_vehicles'class)    begin       ada.text_io.put_line( "testing vehicle" );       ada.text_io.put_line( start(vehicle) );    end;     tractor0 : tractor;    car0 : car;  begin    testvehicle(tractor0);   testvehicle(car0);  end interface_test; 

compiling , running:

[22] marc say: gnatmake interface_test.adb gcc -c interface_test.adb gnatbind -x interface_test.ali gnatlink interface_test.ali [23] marc say: ./interface_test testing vehicle tractor starting! testing vehicle car starting! 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -