Think about function overloading as a case where we have multiple function with same name but individually they grab as parameters different data types.
Now, when we use those functions, and we pass specific values as inputs/arguments, function that must be activated will be automatically called.
It's basically automatic recognition.
In this case we have two function with same name printStuff() but they demand different parameters. Operations are same, but inputs must be different:
#include <iostream>
#include <cstdlib>
using namespace std;
void printStuff(int container) {
cout << container << endl;
}
void printStuff(char container) {
cout << container << endl;
}
int main() {
int x = 50;
char c = 'A';
printStuff(x);
printStuff(c);
return 0;
}
Result:
50
A
Process returned 0 (0x0) execution time : 0.326 s
Press any key to continue.
Very simple but extremelly useful.
No comments:
Post a Comment