Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.