Types of overloading include method overloading and operator overloading.
Method Overloading is where different functions with the same name are invoked based on the data types of the parameters passed or the number of parameters. Method overloading is a type of polymorphism and is also known as Parametric Polymorphism.
Operater Overloading allows an operator to behave differently based on the types of values used. For example, in some languages the + operator is used both to add numbers and to concatenate strings. Custom operator overloading is sometimes referred to as ad-hoc polymorphism.
Delphi Overloading
Delphi supports both method and operator overloading.
For method overloading, you use the overload keyword (all versions of the routine must include the overload keyword). The compiler chooses the correct method first based on the number of parameters, then on the type so you can have two overloaded methods with the same number of parameters so long as at least one parameter is different.
Another form of method overloading is with the use of default parameters, a shortcut syntax, where you specify a default parameter value.
Delphi also supports operator overloading with some operators.
Default Parameters and Overloading
Delphi introduced default parameters with Delphi 4. Default parameters is a form of overloading that allows you to specify a default for a parameter which is used when the routine is called without that particular parameter.
For example, the following function demonstrates default parameter overloading. It allows you to add up to four numbers:
function Add(a, b: integer; c:integer=0; d:integer=0): Integer;
begin
Result := a+b+c+d;
end;
To test our new function, add the following code to a button click event:
ShowMessage('2+2=' + IntToStr(Add(2,2)));
ShowMessage('2+2+3=' + IntToStr(Add(2,2,3)));
ShowMessage('2+2+3+4=' + IntToStr(Add(2,2,3,4)));
Delphi 2009 Working Example
Place a button on a form and add the following functions and the procedure.
//First simple method adds 2 integers.
function Add(a, b: integer): Integer; overload;
begin
Result := a+b;
end;
//Same but allows strings and returns a string.
function Add(a, b: String): String; overload;
begin
Result := IntToStr(StrToInt(a) + StrToInt(b));
end;
//Overload with an extra string parameter
//and now Add returns a string in this form!
function Add(const msg: String; a, b: integer): String; overload;
begin
Result := msg + IntToStr(a+b);
end;
//This function adds a 3rd and 4th
//optional parameter.
function Add(const msg: String; a, b, c: integer; d:integer=0): String; overload;
begin
Result := msg + IntToStr(a+b+c+d);
end;
//You can even overload a function with
//a procedure (or vice versa).
procedure Add(a: integer; b:string); overload;
begin
ShowMessage(IntToStr(a) + '+' + b + '=' + IntToStr(a + StrToInt(b)));
end;
Now test out our overloaded Add routine (it's a routine because it is both a function and procedure!). Add the following code to a buton click event:
//2 integer parameters, returns an integer.
ShowMessage('2+2=' + IntToStr(Add(2,2)));
//2 string parameters, returns a string.
ShowMessage('3+3=' + Add('3', '3'));
//a string parameter and 2 integer parameters, returns a string.
ShowMessage(Add('4+4=', 4, 4));
//a string parameter and 3 integer parameters, returns a string.
ShowMessage(Add('2+2+3=', 2, 2, 3));
//a string parameter and 4 integer paremeters, returns a string.
ShowMessage(Add('2+2+3+4=', 2, 2, 3, 4));
//Use the procedure version.
Add(3, '4');