You are on page 1of 24

Method overloading &

Dynamic Method approach


PRESENTATION BY::
GROUP#1( ASIM,NOMAN,ABDUL,JUNAID )
PRESENTED TO: SIR NAEEM.
Presenter # 1

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

 In this type we define two functions

 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 sum (int x, int y)


 {
 cout << x+y;
 }

 int sum(int x, int y, int z)


 {
 cout << x+y+z;
 }
Example (continued)
 Here sum() function is overloaded, to have two and three arguments. Which sum() function will
be called, depends on the number of arguments.

 int main()
 {
 sum (10,20); // sum() with 2 parameter will be called (x and Y)

 sum(10,20,30); //sum() with 3 parameter will be called (x, y and z)


 }
Presenter#2
Noman Afzal(1522)
Having same name but different data types of arguments

 In this type of overloading we define two or more functions with same name and
same number of parameters.

 but the type of parameter is different.

 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.

 Dynamic binding is also known as late binding or run-time binding.


 int Square(int x)
 { return x*x; }
 int Cube(int x)
 { return x*x*x; }
 int main()
 {
 int x =10;
 int choice;
 do
 {
 cout << "Enter 0 for square value, 1 for cube value: ";
 cin >> choice;
 }
Example(continued)
 while (choice < 0 || choice > 1);
 int (*ptr) (int);
 switch (choice)
 {
 case 0: ptr = Square; break;
 case 1: ptr = Cube; break;
 }
 cout << "The result is: " << ptr(x) << endl;
 return 0; }

Result

 Enter 0 for square value, 1 for cube value:0

 The result is:100

 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>

 using namespace std;

 class Animal
 {
 public:
 virtual void sound()
 {
 cout << "This is parent class" << endl;
 }
 };
Example(continued)

 class Dogs : public Animal


 {
 public:
 void sound()
 {
 cout << "Dogs bark" << endl;
 }
 };
Example(continued)
 int main()
 {
 Animal *a;
 Dogs d;
 a= &d;
 a -> sound();
 return 0;
 }
Output

 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.

You might also like