Professional Documents
Culture Documents
RAJA ASIM
Method OR FUNCTION
OVERLOADING
IN PROGRAMMING Languages like C++ or java function or method overloading basically
happens when two or more (function’s or method’s) have the same name but different
(parameters or arguments).
For example :
int test() { }
int test(int a) { }
float test(double a) { }
int test(int a, double b) { }
As we can see in the above examples there are four functions having the same name that is Test.
But they have different types of arguments and different number of argument’s
Taking these point’s into thought we can say that there are two ways a function can be
overloaded
having same name but different number of arguments
Having same name but different data types of arguments
Having same name but different number of arguments
with same names but different number of parameters of the same type.
For example, in the below mentioned program we have made two sum()
functions to return sum of two and three integers.
Example (continued)
int main()
{
sum (10,20); // sum() with 2 parameter will be called (x and Y)
In this type of overloading we define two or more functions with same name and
same number of parameters.
For example in the program below, we have two sum() function, first one gets two
integer arguments and second one gets two double arguments.
int sum(int x,int y)
{
cout<< x+y;
}
double sum(double x,double y)
{
cout << x+y;
}
int main()
{
sum (10,20);
sum(10.5,20.5);
}
Default arguments
When we mention a default value for a parameter while declaring the function, it
is said to be as default argument.
In this case, even if we make a call to the function without passing any value for
that parameter, the function will take the default value specified.
For example
sum(int x,int y=0)
{
cout << x+y;
}
Here we have provided a default value for y, during function definition.
int main()
{
sum(10);
sum(10,0);
sum(10,10);
}
Example(continued)
Output
10 10 20
First two function calls will produce the exact same value.
for the third function call, y will take 10 as value and output will become 20.
By setting default argument, we are also overloading the function. Default arguments also allow
you to use the same function in different situations just like function overloading.
Presenter # 3
Abdul Ahad
Dynamic method approach
or Dynamic binding
Dynamic binding also called dynamic dispatch is the process of linking procedure call to a
specific sequence of code (function) at run-time.
It means that the code to be executed for a specific procedure call is not known until run-time.
In the above OOPs example the functions "Square" and "Cube" are called only at
runtime based on the value given for "choice".
Then a pointer "ptr" is used to call the appropriate function to get the result.
Presenter # 4
Junaid Sajid
When do we use it?
In C++, late binding (also called "dynamic binding") normally happens when the
virtual keyword is used in a function declaration.
C++ then creates a so-called virtual table, which is a look-up table for such
functions that will always be consulted when they are called.[
Example:
#include <iostream>
class Animal
{
public:
virtual void sound()
{
cout << "This is parent class" << endl;
}
};
Example(continued)
Since the function sound() of the base class is made virtual, the compiler now
performs late binding for this function.
Now, the function call will be matched to the function definition at runtime.
Since the compiler now identifies pointer a as referring to the object 'd' of the
derived class Dogs, it will call the sound() function of the class Dogs.