Function overloading
Function overloading:-
- Function overloading:-
- Data science & data analyst
- Data Cleaning and Preprocessing in Data ScienceAdvanced Data Analysis Techniques: Unlocking Insights from DataData Visualization Techniques in Data ScienceDescriptive Statistics in Data SciData Science Tools and TechniquesScope of Data ScienceWhy learn Data Science? | Why Data Science?Impact of Data ScienceThe Importance of Data in Science | Introduction to Data ScienceWhat is Data Analysis | Data Analyst for Beginners
- 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 languageDifference Between OOP And POP || Different Between C and C++Characteristics of C++Interesting fact about C++ || Top 10 interesting fact about C++C++ Program StructureWriting 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 languageHow to Add two number in C++Polymorphism in C++Compile Time Polymorphism in C++Function overloading in C++Operator Overloading in C++What are Control Structures in C++ || Understanding Control Structures in C++ | How to use if, else, switchWhat are Functions and Recursion in C++ | How to Defining and Calling FunctionsClass in C++Object in C++
- Why algorithm | The Importance of Algorithms in Modern Technology
- Which is better | BSc in Computer Science or BTech?
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
// 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
#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();
|