In this article method overloading we give the information about method overloading is an important feature in which two or more functions have the same (same) name but their parameters are different.

Method overloading in c++:

Introduction

The C++ language has the facility of assigning the same name to more than one function according to a condition. By setting the same name of the function, the number of parameters in each function is kept separately. This property of C++ language is called Function Overloading. Function overloading is used when multiple functions with the same name perform different actions on different types of parameters.

In C++, Function overloading is an important feature in which two or more functions have the same (same) name but their parameters are different.

In other words, “In C++, many functions may have the same name but have different parameters. This is called function overloading.

Function overloading is also called compile-time polymorphism.

The main advantage of function overloading is that it increases the readability of the program because we do not need different names for the same task.

for example –

// same name different arguments

int demo() { }

int demo(int a) { }

float demo(double a) { }

int demo(int a, double b) { }

In the above example, all 4 functions are overloaded. Their names are the same but their arguments are different.

How to do Function Overloading in C++?

Functions are overloaded in two ways.

1. By changing the number of arguments.

2. By changing the type of Arguments.

By changing the number of arguments –

In this type of function overloading, the number of arguments of the function is different.

Its example-

#include <iostream>

using namespace std;

int add (int a, int b)

{

cout << a + b << endl;

 return 0;

}

int add (int a, int b, int c)

{

cout << a + b + c << endl;

return 0;

}

int main ()

{

add (1, 3);

add (5, 2, 3);

}

Its output-410In the above example, we have overloaded the add() function by changing the number of arguments. First we have defined add() with two parameters and then we have defined add() with three parameters.

By changing the type of argument –

In this method, the data types of the parameters of the function are different.

Its example-

#include <iostream>

using namespace std;

int add(int x, int y) // first definition

{

cout<< x+y << endl;

return 0;

}

 float add(float a, float b)

{

cout << a+b << endl;

return 0;

}

double add(double x, double y)

{

cout << x+y << endl;

    return 0;

}

int main()

{

add(30, 10);

add(21.45f, 38.4f);

add(41.24, 21.234);

}

Its output -4059.8562.474In the above example, we have defined the add() function three times. We have used int in the first, float in the second and double parameters in the third.

Advantage of Function overloading in C++

Its benefits are as follows:-

1. We use function overloading to save memory, improve consistency and readability of programs.

2. Using this, we can create many functions of the same name.

3. It increases the speed of execution of our program.

4. This increases the reusability of the code, that is, we can use one code in many places.

5. We can easily maintain the code.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *