Content deleted Content added
m Disambiguating links to Object-orientation (link changed to Object-oriented programming) using DisamAssist. |
|||
(One intermediate revision by one other user not shown) | |||
Line 9:
In some [[programming language]]s, '''function overloading''' or '''method overloading''' is the ability to create multiple [[subprogram|functions]] of the same name with different implementations. Calls to an overloaded function will run a specific implementation of that function appropriate to the context of the call, allowing one function call to perform different tasks depending on context.
== Basic definition ==
For example, {{mono|doTask()}} and {{nowrap|{{mono|doTask(object o)}}}} are overloaded functions. To call the latter, an [[object (computer science)|object]] must be passed as a [[parameter (computer science)|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 function, which would result in an ''ambiguous call'' error, as the [[compiler]] wouldn't know which of the two methods to use.
Line 39 ⟶ 40:
* [[Python (programming language)|Python]]
Languages
==Rules in function overloading==
Line 86 ⟶ 87:
==Constructor overloading==
[[Constructor (object-oriented programming)|Constructors]], used to create instances of an object, may also be overloaded in some [[Object-oriented programming|object-oriented]] [[programming language]]s. 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 to be implemented as overloaded functions. In [[C++]], [[default constructor]]s take no parameters, instantiating the object [[Instance variable|members]] with their appropriate default values, "which is normally zero for numeral fields and empty string for string fields".<ref>{{cite book |last1=Chan |first1=Jamie |title=Learn C# in One Day and Learn It Well |date=2017 |isbn=978-1518800276 |page=82 |edition=Revised}}</ref> For example, a default constructor for a restaurant bill object written in C++ might set the tip to 15%:
<syntaxhighlight lang=Cpp>
|