{$define virtual} {$N+} {R+} uses crt; type PT = ^T; T = object constructor create; destructor done; {$ifdef virtual} virtual;{$endif} end; PTint = ^Tint; Tint = object(T) value: integer; procedure init(X: integer); procedure print; {$ifdef virtual} destructor done; virtual;{$endif} end; PTfloat = ^Tfloat; Tfloat = object(T) value: double; procedure init(X: double); procedure print; {$ifdef virtual} destructor done; virtual; {$endif} end; constructor T.create; begin end; destructor T.done; begin end; procedure Tint.init(X: integer); begin value := X end; procedure Tint.print; begin write(value:4) end; {$ifdef virtual} destructor Tint.done; begin end; {$endif} procedure Tfloat.init(X: double); begin value := X; end; procedure Tfloat.print; begin write(value:8:4) end; {$ifdef virtual} destructor Tfloat.done; begin end; {$endif} const maxSize = 100; type TArr = object arr: array[1 .. maxSize] of PT; num:byte; constructor init; procedure set_index(i: integer; const p: PT); procedure count_each(const p: T); end; constructor TArr.init; var i:byte; begin for i:=1 to maxSize do arr[i]:=nil; end; procedure TArr.set_index(i: integer; const p: PT); begin if i<= maxSize then begin if arr[i]=nil then arr[i]:=p else begin dispose(arr[i],done); arr[i]:=p; end; end; end; procedure TArr.count_each(const p: T); var i, count: integer; begin count := 0; for i := 1 to maxSize do if (arr[i]<>nil) and (typeof(arr[i]^) = typeof(p)) then inc(count); writeln('count = ', count); end; var obj_int: PTint; obj_float:PTfloat; any_arr: TArr; mem:longint; begin clrscr; mem:=memavail; any_arr.init; obj_int := new(PTint,create); obj_int^.value := 3; any_arr.set_index(1, obj_int); obj_float := new(PTfloat,create); obj_float^.value := 3.5; any_arr.set_index(2, obj_float); any_arr.count_each(TFloat(obj_float^)); dispose(obj_float,done); dispose(obj_int,done); writeln('Difference : ',mem-memavail); readln; end.