Skip to content

Instantly share code, notes, and snippets.

@kateolenya
Last active April 26, 2019 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kateolenya/17e470142c3953ea3ecf87ae6cc0f8c1 to your computer and use it in GitHub Desktop.
Save kateolenya/17e470142c3953ea3ecf87ae6cc0f8c1 to your computer and use it in GitHub Desktop.
Example of overloaded function in C++
#include <iostream>
using namespace std;
void custom_add(int a, int b) {
cout << "Int result = " << (a + b) << endl;
}
void custom_add(int a, int b, int c) {
cout << "Triple int result = " << (a + b + c) << endl;
}
void custom_add(float a, float b) {
cout << "Float result = " << (a + b) << endl;
}
int main() {
int c = 1;
int e = 2;
int d = 3;
float a = 10.1;
float r = 11.2;
custom_add(c, e); // by input type & count
custom_add(c, e, d); // by input type & count
custom_add(a, r); // by input type
// custom_add(c, r); // call of overloaded function is ambiguous
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment