Last active
April 26, 2019 09:57
-
-
Save kateolenya/17e470142c3953ea3ecf87ae6cc0f8c1 to your computer and use it in GitHub Desktop.
Example of overloaded function in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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