RAD Studio |  |

Example of Declaring Methods
C++ | Delphi
Collapse All

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 .H file:

Copy Code

    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:

Copy Code

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

Delphi

Copy Code

    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;

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!