メソッド宣言の例

提供: RAD Studio
移動先: 案内検索

メソッドの宣言 への移動


次のコードは、2 つの新規メソッド(protected メソッドと仮想 public メソッド)を定義しているコンポーネントの例です。

C++

これはヘッダー ファイル内のインターフェイス定義です。

class PACKAGE TSampleComponent : public TControl
{
protected:
    void __fastcall MakeBigger();
public:
    virtual int __fastcall CalculateArea();
  .
  .
  .
};

メソッドを実装するユニットの .CPP ファイルのコードは、たとえば次のとおりです。

void __fastcall TSampleComponent::MakeBigger()
{
  Height = Height + 5;
  Width = Width + 5;
}
int __fastcall TSampleComponent::CalculateArea()
{
  return Width * Height;
}

Delphi

type
  TSampleComponent = class(TControl)
  protected
    procedure MakeBigger;                              { declare protected static method }
  public
    function CalculateArea: Integer; virtual;            { declare public virtual method }
  end;
.
.
.
implementation
.
.
.
procedure TSampleComponent.MakeBigger;                          { implement first method }
begin
  Height := Height + 5;
  Width := Width + 5;
end;
function TSampleComponent.CalculateArea: Integer;              { implement second method }
begin
  Result := Width * Height;
end;

関連項目