Function overloading

Function overloading

Function overloading or method overloading is a feature found in various programming languages such as Ada, C#, VB.NET, C++, D and Java that allows the creation of several methods with the same name which differ from each other in terms of the type of the input and the type of the output of the function.

For example, doTask() and doTask(object O) are overloaded methods. To call the latter, an object must be passed as a parameter, whereas the former does not require a parameter, and is called with an empty parameter field. A common error would be to assign a default value to the object in the second method, which would result in an ambiguous call error, as the compiler wouldn't know which of the two methods to use.

Another appropriate example would be a Print(object O) method. In this case one might like the method to be different when printing, for example, text or pictures. The two different methods may be overloaded as Print(text_object T); Print(image_object P). If we write the overloaded print methods for all objects our program will "print", we never have to worry about the type of the object, and the correct function call again, the call is always: Print(something).

Method overloading is usually associated with statically-typed programming languages which enforce type checking in function calls. When overloading a method, you are really just making a number of different methods that happen to have the same name. It is resolved at compile time which of these methods are used.

Method overloading should not be confused with virtual functions, where the correct method is chosen at runtime.[citation needed]

Contents

Constructor overloading

Constructors, used to create instances of an object, may also be overloaded in some object oriented programming languages. Because in many languages the constructor’s name is predetermined by the name of the class, it would seem that there can be only one constructor. Whenever multiple constructors are needed, they are implemented as overloaded functions. A default constructor takes no parameters, instantiating the object members with a value zero.[1] For example, a default constructor for a restaurant bill object written in C++ might set the Tip to 15%:

Bill()
{ 
    tip = 15.0; 
#include
}

The drawback to this is that it takes two steps to change the value of the created Bill object. The following shows creation and changing the values within the main program:

Bill cafe;
cafe.tip = 10.00;
cafe.total = 4.00;

By overloading the constructor, one could pass the tip and total as parameters at creation. This shows the overloaded constructor with two parameters:

Bill(double setTip, double setTotal)
{ 
    tip = setTip; 
    total = setTotal; 
}

Now a function that creates a new Bill object could pass two values into the constructor and set the data members in one step. The following shows creation and setting the values:

Bill cafe(10.00, 4.00);

This can be useful in increasing program efficiency and reducing code length.

Caveats

If a method is designed with an excessive number of overloads, it may be difficult for developers to discern which overload is being called simply by reading the code. This is particularly true if some of the overloaded parameters are of types that are inherited types of other possible parameters (for example "object"). An IDE that can do the overload resolution and display (or navigate to) the correct overload can resolve this issue.

See also

External links

  • Bertrand Meyer: Overloading vs Object Technology, in Journal of Object-Oriented Programming (JOOP), vol. 14, no. 4, October-November 2001, available online

References

  1. ^ Computer Science A Structured Approach Using C++ by Behrouz A. Forouzan and Richard F. Gilberg

Wikimedia Foundation. 2010.

Игры ⚽ Поможем сделать НИР

Look at other dictionaries:

  • Function Overloading —   [dt. »Funktionsüberladung«], Überladen …   Universal-Lexikon

  • Overloading —   [dt. Überladen] (Function Overloading, dt. »Funktionsüberladung«), ein Weg, um bei der objektorientierten Programmierung (z. B. mit den Programmiersprachen Ada, C++ oder Delphi) bereits vorhandenen Elementen (z.. Operatoren, Funktionen,… …   Universal-Lexikon

  • Overloading — See also: Overload The terms overloading and overloaded may refer to: Constructor and function/method overloading, in computer science, a type of polymorphism where different functions with the same name are invoked based on the data types of the …   Wikipedia

  • Function object — A function object, also called a functor or functional, is a computer programming construct allowing an object to be invoked or called as if it were an ordinary function, usually with the same syntax.Function objects are unrelated to functors in… …   Wikipedia

  • Operator overloading — Theories and practice of polymorphism Double dispatch Multiple dispatch Operator overloading Polymorphism in computer science Polymorphism in OOP Subtyping …   Wikipedia

  • Method overloading — is a feature found in various programming languages such as Ada, C#, C++ and Java that allows the creation of several functions with the same name which differ from each other in terms of the type of the input and the type of the output of the… …   Wikipedia

  • Type polymorphism — In computer science, polymorphism is a programming language feature that allows values of different data types to be handled using a uniform interface. The concept of parametric polymorphism applies to both data types and functions. A function… …   Wikipedia

  • C++ — The C++ Programming Language, written by its architect, is the seminal book on the language. Paradigm(s) Multi paradigm:[1] procedural …   Wikipedia

  • Double dispatch — Theories and practice of polymorphism Double dispatch Multiple dispatch Operator overloading Polymorphism in computer science Polymorphism in OOP Subtyping …   Wikipedia

  • Multiple dispatch — Theories and practice of polymorphism Double dispatch Multiple dispatch Operator overloading Polymorphism in computer science Polymorphism in OOP Subtyping …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”