unit Geometry;
{$mode objfpc}{$H+}
interface
uses
SysUtils;
type
TGeomShape = class
strict private
FName: string;
public
constructor Create(const AName: string);
function GetArea: Double; virtual; abstract;
function GetPerimeter: Double; virtual; abstract;
function ToString: string; override;
property Name: string read FName;
end;
TCircle = class(TGeomShape)
strict private
FRadius: Double;
public
constructor Create(const AName: string; ARadius: Double);
function GetArea: Double; override;
function GetPerimeter: Double; override;
property Radius: Double read FRadius;
end;
TRectangle = class(TGeomShape)
strict private
FWidth: Double;
FHeight: Double;
public
constructor Create(const AName: string; AWidth, AHeight: Double);
function GetArea: Double; override;
function GetPerimeter: Double; override;
property Width: Double read FWidth;
property Height: Double read FHeight;
end;
implementation
{ TGeomShape }
constructor TGeomShape.Create(const AName: string);
begin
inherited Create;
FName := AName;
end;
function TGeomShape.ToString: string;
var
NamePart: string;
begin
// Выравниваем имя по левому краю (15 символов), чтобы двоеточия шли в ряд
NamePart := Format('%-15s', [FName + ':']);
Result := NamePart + Format('S=%s, P=%s', [
FormatFloat('0.00', GetArea),
FormatFloat('0.00', GetPerimeter)
]);
end;
{ TCircle }
constructor TCircle.Create(const AName: string; ARadius: Double);
begin
inherited Create(AName);
if ARadius < 0 then
FRadius := 0
else
FRadius := ARadius;
end;
function TCircle.GetArea: Double;
begin
Result := Pi * FRadius * FRadius;
end;
function TCircle.GetPerimeter: Double;
begin
Result := 2 * Pi * FRadius;
end;
{ TRectangle }
constructor TRectangle.Create(const AName: string; AWidth, AHeight: Double);
begin
inherited Create(AName);
if AWidth < 0 then FWidth := 0 else FWidth := AWidth;
if AHeight < 0 then FHeight := 0 else FHeight := AHeight;
end;
function TRectangle.GetArea: Double;
begin
Result := FWidth * FHeight;
end;
function TRectangle.GetPerimeter: Double;
begin
Result := 2 * (FWidth + FHeight);
end;
end.