Polymorphic copy: Difference between revisions

Content added Content deleted
No edit summary
Line 584: Line 584:
<pre>I'm the instance of S p: Y23
<pre>I'm the instance of S p: Y23
I'm the instance of S p: X23</pre>
I'm the instance of S p: X23</pre>


=={{header|Delphi}}==
{{trans|C#}}
<lang delphi>program PolymorphicCopy;

type
T = class
function Name:String; virtual;
function Clone:T; virtual;
end;

S = class(T)
function Name:String; override;
function Clone:T; override;
end;

function T.Name :String; begin Exit('T') end;
function T.Clone:T; begin Exit(T.Create)end;

function S.Name :String; begin Exit('S') end;
function S.Clone:T; begin Exit(S.Create)end;

procedure Main;
var
Original, Clone :T;
begin
Original := S.Create;
Clone := Original.Clone;

WriteLn(Original.Name);
WriteLn(Clone.Name);
end;

begin
Main;
end.</lang>
{{out}}
<lang>S
S</lang>


=={{header|E}}==
=={{header|E}}==