#REDIRECT [[function overloading]] {{R from merge}}
To ''overload'' a [[method (computer science)|method]] in [[programming]] is to have two or more methods with the same name, but are distinguished by the '''number and/or type''' of [[variable]]s they require. Generally methods, [[operator]]s, and [[constructor (computer science)|constructors]] are overloaded in programming. Overloading is a feature available only to object oriented programming languages like C++,Java. However, in case Java, operators can not be overloaded.
==Overloading Methods==
For example, ''doTask()'' and ''doTask(object O)'' are '''overloaded''' methods. To call the latter, an [[Object (computer science)|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, this would result in an ''ambiguous call'' error, as the compiler wouldn't know which of the two methods to use.
Another example would be a ''Print(object O)'' method. In this case we would like the method to be different when printing, for example, text and different for pictures. We write the two different methods as overloaded: ''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 [[subroutine|function]] call again, the call is always: ''Print(something)''.
==Overloading Operators ==
It is also possible to overload operators (such as plus, minus, multiply, divide), comparisons ( such as >, <, =), input and output, et al. Overloading operators serves a purpose when performing math and other operations on uncommon types, especially user defined [[data types]]. One could overload the addition operator to add two dates together (in the context of the following one would be adding two time spans together, not necessarily actual dates, and the code doesn't allow for months with less than 31 days). This would be accomplished by something similar to the following [[syntax]] (in C++):
Date Date::operator+(int Month, int Day, int Year)
{
Date temp;
temp.Year += Year;
temp.Day += Day;
if temp.Day > 31
{
temp.Day - 31;
temp.Month + 1;
}
temp.Month += Month;
if temp.Month > 12
{
temp.Month - 12;
temp.Year + 1;
}
}
Addition is a [[binary operation|binary]] operation, which means it has a left and right [[operand]]. In C++, the ''temp'' object in this case is the left operand and the arguments being passed are the right operands. Note that a [[unary operation|unary]] operator would receive no arguments since it doesn't have any operands.
The [[compiler]] would be able to differentiate the two addition operators because one is passed three parameters instead of the normal addition which is passed just one. As mentioned above however, methods are overloaded based on their number AND type of parameters.
Another overloaded addition operator could be overloaded with three parameters IF the data types passed are different too. The compiler would differentiate those two by their data types. The following shows how it would be setup using an [[angle]] as a data type with [[Degree (angle)|Degree]]-[[minute of arc|Minute]]-[[arcsecond|Second]] notation ''within the same program''.
Angle Angle::Operator+(double Degree, double Minutes, double Seconds)
{
''code''
}
The different data types, one being integers, the other being doubles, allows this to be valid in the compiler. Any number of combinations could be created, as long as they don't have the same number of parameters of the same data types.
==Overloading Constructors==
The third type of overloading can be done with [[Constructor (computer science)|constructors]], which are used to create an instance of an object. The same principle applies that it's the '''number and/or types''' of parameters passed into the constructor that decides the overloading.
A default constructor usually doesn't have any parameters passed, normally just setting the objects [[Instance variable|members]] to certain values. For example, a default constructor for a restaurant bill object might set the Tip to 15%:
Bill()
{ tip = 15.0, total = 0.0 }
The drawback to this is that is 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, you could pass parameters into it to set the tip or total to a given value 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 way of programming can be very useful in increasing program efficiency and reducing code in the long run. For every Bill object the user creates, it can be done in one step using the same piece of code over.
{{comp-sci-stub}}
[[Category:Computer science stubs]]
[[Category:Method (computer science)]]
|