|
 |  | Section 9: Object Oriented Programming (OOP) | |
|
|
You are in Section 9 of 9, Article 9.3 of 9.7
Function Overloading
When using multiple constructors for a class, you are actually overloading functions. Operators and regular non-class functions can also be overloaded.
To overload a regular non-class function, simply create multiple functions using the same function name but with each having a different signature, which is the name, number of parameters, order and types of the parameters, and the class to which it belongs (does not include the return type).
The overloaded functions must have different parameter lists so the compiler knows which function to use depending on the order and types of the arguments.
For example, consider the following overloaded add() function:
For another example of overloaded functions, study the following complete program which demonstrates the use of "swap" functions:
Here is a comlete program example:
This swap( ) function is defined for integers, floating point values (double), and strings. All three may be included in the same program. When the function is called within the program, the compiler will determine which function to use by evaluating the parameter list of the calling function.
Just like functions, certain operators can also be overloaded.
Read on for more...
Next: Operator Overloading
You are in Section 9 of 9, Article 9.3 of 9.7
[Back to Top]
|
|
|
|

|
|