What is function overloading in C++?

Many functions can share one name when their parameter lists are different (number or types). The compiler chooses the best match at compile time.

#include <iostream>
#include <string>

void show(int x)               { std::cout << "int " << x << '\n'; }
void show(double x)            { std::cout << "double " << x << '\n'; }
void show(const std::string& x) { std::cout << "string " << x << '\n'; }

int main() {
    show(5);
    show(2.5);
    show(std::string("hello"));
}
Output

int 5
double 2.5
string hello

See Function Overloading and Default Arguments.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.