0% found this document useful (0 votes)
23 views

C++ Lecture-8

Function overloading allows multiple functions to have the same name but different parameters. The parameters must differ in number, type, or order. Overloading is useful because it allows related functions to have the same name, making code more readable. However, overloading does not truly exist at the machine code level, as compilers uniquely rename functions to avoid conflicts. While overloading syntax exists in source code, compilers eliminate it by name mangling when converting to machine code.

Uploaded by

Harsh Arya
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

C++ Lecture-8

Function overloading allows multiple functions to have the same name but different parameters. The parameters must differ in number, type, or order. Overloading is useful because it allows related functions to have the same name, making code more readable. However, overloading does not truly exist at the machine code level, as compilers uniquely rename functions to avoid conflicts. While overloading syntax exists in source code, compilers eliminate it by name mangling when converting to machine code.

Uploaded by

Harsh Arya
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

01

02

LECTURE 8
04
Today’s Agenda
01 Function Overloading

02 Benefit Of Overloading

03 Does Overloading Really Exist ?


05
Function Overloading
 In C++, if within the same scope(class or program) we have declared 2 or more
functions with the same name, then we say that it is Function 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

You might also like