We can have functions that have different parameter lists that have the same name
This is a great use of abstraction since as a developer all i need to think is display and pass in whatever information i need. I don’t have to keep track of dozens of different function names.
In SWE, we have a principal called polymorphism, which means many forms for the same concept. This is an example of polymorphism.
The parameter list for these functions must be different so the compiler can tell them apart. Once these functions are implemented, I can call display and pass in my data. The compiler will check the type of the argument in the function and match it to one of the available overloaded display functions.
If it can’t match it or if it can’t convert the argument to one that matches, then we get a compiler error.
Important - There is one restriction to function overloading. The return type is not considered when the compiler is trying to determine which function to call.
intget_value();doubleget_value();// Errorcout<<get_value()<<'\n';// which one?
Here we have two overloaded functions, both called get_value and both expect no parameters. The only difference is that one returns an integer and the other a double. This will produce a compiler error since the only difference is the return type.
Overloading function is used extensively in object-oriented design.
Top comments (0)
Subscribe
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)