Exemple de déclaration de méthodes

De RAD Studio
Aller à : navigation, rechercher

Remonter à Création de méthodes - Index

Le code suivant montre un composant qui définit deux nouvelles méthodes, l'une est déclarée protected et l'autre public virtuelle.

C++

Il s'agit de la définition d'interface contenue dans le fichier d'en-tête :

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

Voici le code qui figure dans le fichier .CPP de l'unité qui implémente les méthodes :

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;

Voir aussi