Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Overloading | Functions
C++ Intermediate | Mobile-Friendly

bookOverloading

Function overloading - creating functions with the same names and different parameters. It’s often used when you want to apply the same functionality to variables of various types.

For example, let’s create the function printVar(), which prints the variable:

void printVar(int a) {
    cout << a;
}

Hmm, and what should we do if we want to print the string or double variable? Instead of creating another function, let’s overload this one. Here we overload the printVar() function to work for int, double, and string:

void printVar(int a) {
    cout << a;
}

void printVar(double a) {
    cout << a;
}

void printVar(string a) {
    cout << a;
}

Now we can use printVar() to output the variable's type of int, double, and string. Pay attention that the declaration of the overloaded functions must differ from each other by the types of arguments.

question-icon

Fill gaps:

Function overloading - creating functions with thenames and parameters.

Click or drag`n`drop items and fill in the blanks

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 4. Chapitre 11

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Suggested prompts:

Posez-moi des questions sur ce sujet

Résumer ce chapitre

Afficher des exemples du monde réel

Awesome!

Completion rate improved to 2.94

bookOverloading

Glissez pour afficher le menu

Function overloading - creating functions with the same names and different parameters. It’s often used when you want to apply the same functionality to variables of various types.

For example, let’s create the function printVar(), which prints the variable:

void printVar(int a) {
    cout << a;
}

Hmm, and what should we do if we want to print the string or double variable? Instead of creating another function, let’s overload this one. Here we overload the printVar() function to work for int, double, and string:

void printVar(int a) {
    cout << a;
}

void printVar(double a) {
    cout << a;
}

void printVar(string a) {
    cout << a;
}

Now we can use printVar() to output the variable's type of int, double, and string. Pay attention that the declaration of the overloaded functions must differ from each other by the types of arguments.

question-icon

Fill gaps:

Function overloading - creating functions with thenames and parameters.

Click or drag`n`drop items and fill in the blanks

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 4. Chapitre 11
some-alt