In C++, two or more functions can share the same name as long as their parameter are different (based on number of parameters and data types). Here, the functions are said to be overloaded, and the process is referred to as function overloading.
#include <iostream>
using namespace std;
int add(int i, int j);
int add(int i, int j, int k);
double add(int i, int j, double c);
int main() {
cout << add(10, 20) << endl;
cout << add(10, 20, 30) << endl;
cout << add(10, 20, 25.5) << endl;
return 0;
}
int add(int i, int j) {
return i + j;
}
int add(int i, int j, int k) {
return i + j + k;
}
double add(int i, int j, double k) {
return i + j + k;
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.