WikiGalaxy

Personalize

C++ Function Overloading

Introduction to Function Overloading:

Function overloading in C++ allows the creation of multiple functions with the same name, but with different parameters. This concept enables developers to use the same function name for different purposes based on input parameters.


#include <iostream>

void print(int i) {
    std::cout << "Integer: " << i << std::endl;
}

void print(double f) {
    std::cout << "Float: " << f << std::endl;
}

int main() {
    print(5);
    print(5.5);
    return 0;
}
        

Explanation:

In this example, the function print is overloaded to handle both integer and float inputs. The correct version of the function is called based on the argument type.

Console Output:

Integer: 5

Float: 5.5

Overloading with Different Parameter Counts

Concept:

Functions can also be overloaded by varying the number of parameters. This allows the same function name to perform different operations based on the number of arguments passed.


#include <iostream>

void display() {
    std::cout << "No arguments" << std::endl;
}

void display(int i) {
    std::cout << "Integer: " << i << std::endl;
}

void display(int i, double d) {
    std::cout << "Integer: " << i << ", Double: " << d << std::endl;
}

int main() {
    display();
    display(5);
    display(5, 5.5);
    return 0;
}
        

Explanation:

Here, the display function is overloaded with zero, one, and two parameters. The appropriate function is invoked based on the number of arguments.

Console Output:

No arguments

Integer: 5

Integer: 5, Double: 5.5

Overloading with Different Parameter Types

Concept:

Function overloading can also occur by changing the types of parameters. This allows functions to operate on different data types while maintaining the same name.


#include <iostream>

void show(int i) {
    std::cout << "Integer: " << i << std::endl;
}

void show(char c) {
    std::cout << "Character: " << c << std::endl;
}

int main() {
    show(5);
    show('A');
    return 0;
}
        

Explanation:

In this example, the show function is overloaded to accept either an integer or a character. The function that matches the argument type is executed.

Console Output:

Integer: 5

Character: A

Function Overloading with Default Arguments

Concept:

Functions can be overloaded even when they have default arguments. This allows flexibility in calling functions with varying numbers of arguments.


#include <iostream>

void calculate(int a, int b = 10) {
    std::cout << "Sum: " << a + b << std::endl;
}

void calculate(double a, double b) {
    std::cout << "Product: " << a * b << std::endl;
}

int main() {
    calculate(5);
    calculate(3.5, 2.5);
    return 0;
}
        

Explanation:

In this instance, the calculate function is overloaded to handle both integer addition with a default value and float multiplication. The appropriate version is chosen based on the argument types.

Console Output:

Sum: 15

Product: 8.75

Ambiguity in Function Overloading

Concept:

Function overloading can lead to ambiguity when the compiler cannot determine which function to call. This occurs when overloaded functions have similar signatures.


#include <iostream>

void test(int a, double b) {
    std::cout << "Int and Double" << std::endl;
}

void test(double a, int b) {
    std::cout << "Double and Int" << std::endl;
}

int main() {
    test(5, 5.5); // Ambiguous call
    return 0;
}
        

Explanation:

In this code, the call to test(5, 5.5) is ambiguous because both overloaded functions could match the argument types after implicit conversions.

Console Output:

Error: call of overloaded 'test(int, double)' is ambiguous

Overloading with Reference Parameters

Concept:

Functions can be overloaded using reference parameters, allowing functions to modify the original arguments passed to them.


#include <iostream>

void modify(int &a) {
    a += 10;
}

void modify(double &b) {
    b *= 2;
}

int main() {
    int x = 5;
    double y = 3.5;
    modify(x);
    modify(y);
    std::cout << "x: " << x << ", y: " << y << std::endl;
    return 0;
}
        

Explanation:

In this example, the modify function is overloaded to accept both integer and double references. The function modifies the original values of the arguments.

Console Output:

x: 15, y: 7

Function Overloading with Const Parameters

Concept:

Overloading functions with const parameters allows functions to differentiate based on whether they modify the arguments or not.


#include <iostream>

void process(const int &a) {
    std::cout << "Const Integer: " << a << std::endl;
}

void process(int &a) {
    a *= 2;
    std::cout << "Modified Integer: " << a << std::endl;
}

int main() {
    int x = 5;
    const int y = 10;
    process(x);
    process(y);
    return 0;
}
        

Explanation:

In this example, the process function is overloaded to handle both const and non-const integer references. The function behaves differently based on the constness of the argument.

Console Output:

Modified Integer: 10

Const Integer: 10

Function Overloading and Type Promotion

Concept:

Type promotion in function overloading occurs when a smaller data type is promoted to a larger data type to match the function signature.


#include <iostream>

void compute(float f) {
    std::cout << "Float: " << f << std::endl;
}

void compute(double d) {
    std::cout << "Double: " << d << std::endl;
}

int main() {
    compute(5.5f);
    compute(5.5);
    return 0;
}
        

Explanation:

In this example, the compute function is overloaded for both float and double. The argument 5.5f matches the float version, while 5.5 matches the double version.

Console Output:

Float: 5.5

Double: 5.5

Function Overloading with Pointers

Concept:

Pointers can be used in function overloading to allow functions to operate on memory addresses directly, providing more flexibility in handling data.


#include <iostream>

void analyze(int *p) {
    std::cout << "Pointer to int: " << *p << std::endl;
}

void analyze(double *p) {
    std::cout << "Pointer to double: " << *p << std::endl;
}

int main() {
    int a = 5;
    double b = 5.5;
    analyze(&a);
    analyze(&b);
    return 0;
}
        

Explanation:

In this example, the analyze function is overloaded to accept pointers to both integers and doubles. The function dereferences the pointer to access and print the value.

Console Output:

Pointer to int: 5

Pointer to double: 5.5

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025