C++ Function Overloading

C++ Function Overloading

C++ Function Overloading
Function refers to a segment that groups code to perform a specific task.
In C++ programming, two functions can have same name if number and/or type of arguments passed are different.
These functions having different number or type (or both) of parameters are known as overloaded functions. For example:
int test() { }
int test(int a) { }
float test(double a) { }
int test(int a, double b) { }
Here, all 4 functions are overloaded functions because argument(s) passed to these functions are different.
Notice that, the return type of all these 4 functions are not same. Overloaded functions may or may not have different return type but it should have different argument(s).
// Error code
int test(int a) { }
double test(int b){ }
The number and type of arguments passed to these two functions are same even though the return type is different. Hence, the compiler will throw error.

Example 1: Function Overloading

  1. #include <iostream>
  2. using namespace std;
  3. void display(int);
  4. void display(float);
  5. void display(int, float);
  6. int main() {
  7. int a = 5;
  8. float b = 5.5;
  9. display(a);
  10. display(b);
  11. display(a, b);
  12. return 0;
  13. }
  14. void display(int var) {
  15. cout << "Integer number: " << var << endl;
  16. }
  17. void display(float var) {
  18. cout << "Float number: " << var << endl;
  19. }
  20. void display(int var1, float var2) {
  21. cout << "Integer number: " << var1;
  22. cout << " and float number:" << var2;
  23. }
Output
Integer number: 5
Float number: 5.5
Integer number: 5 and float number: 5.5
Here, the display() function is called three times with different type or number of arguments.
The return type of all these functions are same but it's not necessary.

Example 2: Function Overloading

  1. // Program to compute absolute value
  2. // Works both for integer and float
  3. #include <iostream>
  4. using namespace std;
  5. int absolute(int);
  6. float absolute(float);
  7. int main() {
  8. int a = -5;
  9. float b = 5.5;
  10. cout << "Absolute value of " << a << " = " << absolute(a) << endl;
  11. cout << "Absolute value of " << b << " = " << absolute(b);
  12. return 0;
  13. }
  14. int absolute(int var) {
  15. if (var < 0)
  16. var = -var;
  17. return var;
  18. }
  19. float absolute(float var){
  20. if (var < 0.0)
  21. var = -var;
  22. return var;
  23. }
Absolute value of -5 = 5
Absolute value of 5.5 = 5.5
In the above example, two functions absolute() are overloaded.
Both functions take single argument. However, one function takes integer as an argument and other takes float as an argument.
When absolute() function is called with integer as an argument, this function is called:
  1. int absolute(int var) {
  2. if (var < 0)
  3. var = -var;
  4. return var;
  5. }
When absolute() function is called with float as an argument, this function is called:
  1. float absolute(float var){
  2. if (var < 0.0)
  3. var = -var;
  4. return var;
  5. }

Comments

Popular posts from this blog

C++ Templates ?

C++ Program to Print Number Entered by User

Educational Website