Saturday, April 19, 2025

C++ Function Overloading

Think about function overloading as a case where we have multiple function with same name but individually they grab as parameters different data types.

Now, when we use those functions, and we pass specific values as inputs/arguments, function that must be activated will be automatically called.

It's basically automatic recognition.

In this case we have two function with same name printStuff() but they demand different parameters. Operations are same, but inputs must be different:

#include <iostream>
#include <cstdlib>

using namespace std;

void printStuff(int container) {
    cout << container << endl;
}

void printStuff(char container) {
    cout << container << endl;
}

int main() {

    int x = 50;
    char c = 'A';

    printStuff(x);
    printStuff(c);

    return 0;

}

Result:

50
A

Process returned 0 (0x0)   execution time : 0.326 s
Press any key to continue.

Very simple but extremelly useful.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...