Overload (programming): Difference between revisions

Content deleted Content added
Overloading Methods: Disambiguation link repair - You can help!
+redir
 
(10 intermediate revisions by 8 users not shown)
Line 1:
#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 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 wich 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 >, <, =), et al. Overloading operators serves a purpose when performing math on uncommon types, especially user [[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;
}
 
The previous code sample could be confusing because it doesn't explain where the left and right [[operands]] are. In C++, the ''temp'' object in this case is the left operand and the parameters being passed are the right operands.
 
A [[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.