Tuesday, 30 August 2016

FUNCTION OVERLOADING


We can create as many functions as we want but what will happen if they belong to the same class category, Let's say that there are two function printing some values but of different types.In this case we will be needing two functions. But we can make the functions with the same name but the condition is with the different number of Parameters or arguments. 
It will be more clear with the help of the program.

PROGRAM: 

#include<iostream.h>
#includ<conio.h>

void show(int a)      //Function 1
{
cout<<"This function contains Integer value = "<<a;
}


void show(float b)    //Function 2
{
cout<<"This function contains Float value = "<<b;
}

void main()
{
clrscr();

// Now we will Call the functions
show(2.3);      
show(10);
getch();
}  


OUTPUT:

This function contains Float value = 2.3
This function contains Integer value = 10

Now the first calling was 'show(2.3)'. This was a float value, so the compiler compared both the functions and their Return types and Parameters. When it found the float value, the function 2 was called then. Then the next call 'show(10)' was having an Integer value, so comparing with the parameters it executed the First Function. 

Also :


No comments:

Post a Comment