Function overloading in C++ | what is functional overloading



Function overloading



Function overloading:-
 

                                    Overloading refers to the use of the same thing for different purposes. C++ also permits overloading of functions. This means that we can use the same function name to create functions that perform a variety of different tasks. This is known as function polymorphism in oop.


Basic definition of function overloading


Function overloading is a feature of C++ that allows us to create multiple functions with the same name, so long as they have different parameters,  for example the parameters list of a function myfuncn(int a, float b) is (int, float) which is different from the function myfuncn(float a, int b) parameter list (float, int). Function overloading is a compile-time polymorphism.


Using the concept of function overloading. We can design a family of functions with one function name but with different argument lists. The function would perform different operation depending on the argument list in the function call. The correct function to be invoked is determined by checking the number and type of the argument but not on the function type. For example overloading sum() function that handles different type of data as shown below:

// Declarations
Int add(int a, int b);                     // prototype 1
Int add(int a, int b, int c);           // prototype 2
Double add(double x, double y);  // prototype 3
// Function calls
Cout<<add(5,10);             // uses prototype 1
Cout<<add(5, 10, 15);      // uses prototype 2
Cout<<add(12.5, 7.5);     // uses prototype 3



A function call first matches the prototype having the same number and type of arguments and then calls the appropriate function for execution. A best match be unique.


 The function selection involves the following steps:

1.   
The compiler first tries to find an exact match in which the types of actual arguments are the same, and use that function.
2.   If an exact match is not found, the complier uses the  integeral promotions to the actual arguments, such as,

                Char to int
           Float to double
3.   When either of them fail, the compiler tries to use the build-in conversions to the actual arguments and then uses the function whose match is unique.
4.   If all of steps fails, then the complier will try to the user-defined conversions in combination with integral promotion and build-in conversions to find a unique match. User-defined conversions are often used in handling class and objects.


Example of function overloading:-


#include<iostream.h>
#include<conio.h>
class sum
{
public:
int add(int a, int b)
{
return a+b;
}
int add(int a, int b, int c)
{
return a+b+c;
}
int add(double x, double y)
{
return x+y;
}
};
void main()
{
clrscr();
sum s;
cout<<s.add(5, 10)<<endl;
cout<<s.add(5, 10, 15)<<endl;
cout<<s.add(12.5, 5.5);
getch();

Output:
15
30
18


To learn more you can just click the below topics:

C++

INTRODUCTION OF C++ || Definition of C++
Brief history of C++ || history of C++
Features of C++ || why we use C++ || concept of C++
Concept of OOP || What is OOP || Object oriented programming language
Difference Between OOP And POP || Different Between C and C++
Characteristics of C++
Interesting fact about C++ || Top 10 interesting fact about C++
C++ Program Structure
Writing first program in C++ || how to write hello world in C++
Basic Data Type And Variable In C++
Identifier in C++
Keywords in C++
Token in C++
Comment in C++
Constant in C++
Modifier in C++
Taking User Input in C++ | User input in C++
Input Output Operator In C++
C++ Operators | Operator in programming language
How to Add two number in C++
Polymorphism in C++
Compile Time Polymorphism in C++
Operator Overloading in C++
What are Control Structures in C++ || Understanding Control Structures in C++ | How to use if, else, switch
What are Functions and Recursion in C++ | How to Defining and Calling Functions

Data Science

The History and Evolution of Data Science
Scope of Data Science
Why learn Data Science? | Why Data Science?
Impact of Data Science
The Importance of Data in Science | Introduction to Data Science
What is Data Analysis | Data Analyst for Beginners

Algorithm

Why algorithm | The Importance of Algorithms in Modern Technology

Tech to know

Which is better | BSc in Computer Science or BTech?

Post a Comment

Ask any query by comments

Previous Post Next Post