Example of Declaring Methods

From RAD Studio
Jump to: navigation, search

Go Up to Declaring Methods


The following code shows a component that defines two new methods, one protected method and one public virtual method.

C++

This is the interface definition in the header file:

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

This is the code in the .CPP file of the unit that implements the methods:

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;

See Also