You are on page 1of 14

•Binding

•Polymorphism
•Function Overloading
•Ambiguity in function overloading

Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering


College Ajmer
Binding
 Binding: The process of matching the function call with
the correct function definition by the compiler. There
are two types of bindings-
1. Static binding: By default, matching of function call
with the correct function definition happens at
compile time, is called static/early/compile-time
binding.
2. Dynamic binding: The compiler matches function calls
with the correct definition at the run time, is called
dynamic/late/run-time binding.

Prepared by: Anil Kumar Tailor, Assistant Prof. Engineering College


Ajmer
Polymorphism
 The ability to take more than one form is called
polymorphism.
 An operation may show different behaviors in different
instances.
 Behaviors depends on the type of data used in the
operation.
 For example, In addition operation, for two numbers it gives
sum and for two strings it gives concatenation.
 Two types of polymorphism-
1. Compile-time/static polymorphism
2. Run-time/dynamic polymorphism

Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering


College Ajmer
Types of Polymorphism

Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering


College Ajmer
Function overloading
 The mechanism of using same function name to
create functions that perform different tasks, called
function overloading.
 Perform different tasks, depends on arguments list
in function call.
 Correct function to be invoked is determined by
checking the number and type of the arguments
but not on function return type.

Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering


College Ajmer
Function overloading
 Example - an overloaded add() function
//Declaration
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
double add(int p , double q); //prototype 4
double add(double p , int q); //prototype 5
//function call
cout<<add(5, 10); //uses prototype 1
cout<<add(15, 10.0); //uses prototype 4
cout<<add(12.5, 7.5); //uses prototype 3
cout<<add(5, 10, 15); //uses prototype 2
cout<<add(0.75, 5); //uses prototype 5
Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering
College Ajmer
Function overloading
 The function selection involves the following steps:-
1. Find an exact match in which the types of actual arguments are the
same.
2. Uses integral promotions to the actual arguments such as : char to int
,float to double.
3. Uses the following implicit type conversions to the actual arguments,
if multiple matches, then compiler will give error message.

Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering


College Ajmer
Function overloading
//Program to overload sum() function
int sum(int, int);
int sum(int, int, int);
int main()
{
cout<<“Sum of two numbers is ”<<sum(5, 10);
cout<<“\n”;
cout<<“Sum of three numbers is ”<<sum(10, 20, 30);
return 0;
}

Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering


College Ajmer
Function overloading
int sum(int x, int y)
{
return(x+y);
}
int sum(int a, int b, int c)
{
return(a+b+c);
}
Output-
Sum of two numbers is 15
Sum of three numbers is 60

Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering


College Ajmer
Ambiguity in function overloading
 When the compiler is unable to decide which function it
should invoke first among the overloaded functions, this
situation is known as function overloading ambiguity.
 The compiler does not run the program if it shows
ambiguity error.
 Causes of Function Overloading ambiguity:
1. Type Conversion.
2. Function with default arguments.
3. Function with a pass by reference

Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering


College Ajmer
Ambiguity in function overloading
 Example-Type Conversion.
void function(float);
void function(int);
void function(float x)
{
cout << "Value of x is : " <<x;
}

void function(int y)
{
cout << "Value of y is : " <<y;
}

int main()
{
function(3.4);
return 0;
}

Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering


College Ajmer
Ambiguity in function overloading
 Example-Function with default arguments.

void function(int);
void function(int, int);
void function(int x)
{
cout << "Value of x is : " <<x;
}
void function(int y, int z=12)
{
cout << "Value of y is : " <<y;
cout << "Value of z is : " <<z;
}
int main()
{
function(10);
return 0;
}

Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering


College Ajmer
Ambiguity in function overloading
Example- Function with a pass by reference

void function(int);
void function(int &);
void function(int a)
{
cout << "Value of a is : " <<a;
}
void function(int &b)
{
cout << "Value of b is : " <<b;
}

int main()
{
int x=10;
function(x);
return 0;
}

Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering


College Ajmer
Thank You

Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering


College Ajmer

You might also like