C++ Lecture-8
C++ Lecture-8
02
LECTURE 8
04
Today’s Agenda
01 Function Overloading
02 Benefit Of Overloading
But if we are declaring two or more functions with the same name, then we must
provide some difference in the prototype of these functions and this difference is
in terms of the Arguments.
.
The difference of Arguments can be of 3 types:
1. Difference in number of arguments.
For ex: void volume(int);
void volume(int,int,int);
Function Overloading
2. Difference in data types of arguments.
For ex:
void area(int);
void area(float);
3. Difference in order of arguments.
For ex:
void show(int,float); .
void show(float,int);
Function Overloading
Special Point:
We can never upload 2 or more functions just on the basis of their return type.
This means, that overloaded functions compulsorily must differ with each other
w.r.t their arguments and if they only differ in terms of their return types then
the code will not even compile.
.
So following Declaration will give syntax error:
void show();
int show(); Syntax
float show(); Error
Function Overloading
Why we can’t overload functions just on the basis of their return types:
Exe.- int show();
void show(); This is because , it is not compulsory
int main() To receive or use the value returned by a
function .
{
So the compiler will not be able to
int x; Determine , that whether the call is
.
x=show(); //Perfectly ok Being made to a function with void
show();//Confused?? Return type or to any other function
} Whose return type is not-void, but
We are not receiving its return values
Function Overloading
Example:-
switch(choice)
#include<iostream.h> {
#include<conio.h> case 1:
int s;
void volume(int); cout<<“Enter side of the Cube:”;
void volume(int,int,int); cin>>s;
Int main() . volume(s);
break;
{
case 2:
int choice; int l,b,h;
clrsce(); cout<<“Enter l,b,h of cuboid:”;
cout<<“Select a figure:”; cin>>l>>b>>h;
cout<<endl<<“1. Cube”<<endl<<2. Cuboid:’; volume(l,b,h);
cin>>choice; break;
Function Overloading
default: Output:
cout<<“wrong choice”;
} Select a figure:
getch(); 1. Cube
return 0; 2. Cuboid
void volume(int s) 1
{ . Enter Side of the cube:3
cout<<“vol of cube is”<<s*s*s<<endl;
Vol of cube is 27
}
void volume(int l, int b,int h)
{ cout<<“vol of cuboid is”<<l*b*h<<endl;
}
Function Overloading
What is the benefit of overloading:
1. The overhead of remembering function names does not comes on the programmer
calling the function. He can simply remember just one name and using that he can
call different versions of the functions.
2. The code becomes more symmetrical as well as clean if for similar task we use functions
with same names.
Function Overloading
Does overloading really exist?
Surprisingly, the answer to this question is both yes and no.
But to understand this, we should first recall the way a program is compiled and
executed.
.
We know that the code which we write is called as source code and this source code
is then converted to machine code by the C++ compiler.
But we should also remember that compilers never run any program. They just convert
it to machine code and send it for execution to the OS. But every Os has a restriction
which is that all functions in the machine code version must be uniquely named.
Function Overloading
Thus while converting our source code to the machine code, the C++ compiler converts
all the overloaded function names to some unique name.
Specially for this purpose, it uses a software built into C++ compiler called as name
Mangler.
Thus when the machine code of our program . gets generated then each and every
function becomes uniquely named and overloading gets totally removed.
Hence at the source code level overloading does exists, but at the machine code level no
overloading remains. So the answer to the above question is both yes and no.
End of Lecture 8
For any queries mail us @: [email protected]
Call us @ : 0755-4271659, 7879165533
Thank you