SlideShare a Scribd company logo
FUNCTION
OVERLOADING
1 Ritika sharma
Polymorphism
The word polymorphism is derived from Greek word Poly
which means many and morphos which means forms.
Polymorphism can be defined as the ability to use the same
name for two or more related but technically different tasks.
Eg-woman plays role of daughter,sister,wife,mother etc.
2 Ritika sharma
Overloading in C++
What is overloading
– Overloading means assigning multiple
meanings to a function name or operator
symbol
– It allows multiple definitions of a function with the same
name, but different signatures.
C++ supports
– Function overloading
– Operator overloading
3 Ritika sharma
Why is Overloading Useful?
 Function overloading allows functions that
conceptually perform the same task on
objects of different types to be given the
same name.
 Operator overloading provides a convenient
notation for manipulating user-defined
objects with conventional operators.
4 Ritika sharma
Function Overloading
Is the process of using the same name for two or more
functions
Requires each redefinition of a function to use a different
function signature that is:
different types of parameters,
or sequence of parameters,
or number of parameters
Is used so that a programmer does not have to remember
multiple function names
5 Ritika sharma
Function Overloading
Two or more functions can have the same name but different
parameters
Example:
int max(int a, int b)
{
if (a>= b)
return a;
else
return b;
}
float max(float a, float b)
{
if (a>= b)
return a;
else
return b;
}
6 Ritika sharma
Overloading Function Call Resolution
 Overloaded function call resolution is done by
compiler during compilation
– The function signature determines which definition
is used
 a Function signature consists of:
– Parameter types and number of parameters
supplied to a function
 a Function return type is not part of function signature
and is not used in function call resolution
7 Ritika sharma
void sum(int,int);
void sum(double,double);
void sum(char,char);
void main()
{
int a=10,b=20 ;
double c=7.52,d=8.14;
char e=‘a’ , f=‘b’ ;
sum(a,b); //calls sum(int x,int y)
sum(c,d); //calls sum (double x,double y)
sum(e,f); // calls sum(char x,char y)
}
void sum(int x,int y)
{
vout<<“n sum of integers are”<<x+y;
}
void sum(double x,double y)
{
cout<<“n sum of two floating no are”<<x+y;
}
void sum(char x,char y)
{
cout<<“n sum of characters are”<<x+y;
8 Ritika sharma
Output:
Sum of integers 30
sum of two floating no are 15.66
sum of characters are 195
9 Ritika sharma
Void area(int)
Void area(int,int);
Void area(int,int,int);
Int main()
{
Int side=10,le=5,br=6,a=4,b=5,c=6;
Area(side);
Area(le,br);
Area(a,b,c);
Getch();
Return 0;
}
Void area(int x)
{ cout<<“area is”<<x*x;
}
Void area(int x,int y)
{cout<<“area of rectang;e”=<<x*y;
}
Void area(int x,int y,int z)
{cout<<“volume is”<<x*y*z;
}
10 Ritika sharma
Function Selection Involves following
Steps.
Compiler first tries to find the Exact match in which the type
of argument are the same,and uses that func.
If an exact match is not found,the compiler user the integral
promotions to the actual argument such as,char to int, float
to double.
When either of them fails ,build in conversions are
used(implicit conversion) to the actual arguments and then
uses the function whose match is unique.but if there are
multiple matches,then compiler will generate an error
message.
11 Ritika sharma
For ex: long square(long n)
long square(double x)
Now a func. call such as square(10) will cause an
error because int argument can be converted into
long also and double also.so it will show
ambiguity.
User defined conversion are followed if all the
conversion are failed.
12 Ritika sharma
SCOPE RULES
13 Ritika sharma
Scope
The scope of a variable is the portion of a program where the
variable has meaning (where it exists).
A global variable has global (unlimited) scope.
A local variable’s scope is restricted to the function that
declares the variable.
A block variable’s scope is restricted to the block in which
the variable is declared.
14 Ritika sharma
Understanding Scope
Some variables can be accessed throughout an entire
program, while others can be accessed only in a limited part
of the program
The scope of a variable defines where it can be accessed in a
program
To adequately understand scope, you must be able to
distinguish between local and global variables
15 Ritika sharma
Local variables
Parameters and variables declared inside the definition of a
function are local.
They only exist inside the function body.
Once the function returns, the variables no longer exist!
That’s fine! We don’t need them anymore!
16 Ritika sharma
Block Variables
You can also declare variables that exist only within the body
of a compound statement (a block):
{
int foo;
…
…
}
17 Ritika sharma
Global variables
You can declare variables outside of any function definition –
these variables are global variables.
Any function can access/change global variables.
Example: flag that indicates whether debugging information
should be printed.
18 Ritika sharma
Distinguishing Between Local
and Global Variables
Celebrity names are global because they are known to people
everywhere and always refer to those same celebrities
Global variables are those that are known to all functions in a
program
Some named objects in your life are local
You might have a local co-worker whose name takes
precedence over, or overrides, a global one
19 Ritika sharma
A note about
Global vs. File scope
A variable declared outside of a function is available
everywhere, but only the functions that follow it in the file
know about it.
The book talks about file scope, I’m calling it global scope.
20 Ritika sharma
Block Scope
int main(void) {
int y;
{
int a = y;
cout << a << endl;
}
cout << a << endl;
}
Error – a
doesn’t exist outside
the
block!
21 Ritika sharma

More Related Content

What's hot

Tokens in C++
Tokens in C++Tokens in C++
Tokens in C++
Mahender Boda
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
Sathish Narayanan
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
tanmaymodi4
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
Vineeta Garg
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vishal Patil
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
HalaiHansaika
 
Functions in c
Functions in cFunctions in c
Functions in c
sunila tharagaturi
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
RahulAnanda1
 
Function overloading and overriding
Function overloading and overridingFunction overloading and overriding
Function overloading and overriding
Rajab Ali
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
Sanjit Shaw
 
Command line arguments
Command line argumentsCommand line arguments
Command line arguments
Ashok Raj
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
03062679929
 
Structure in C
Structure in CStructure in C
Structure in C
Kamal Acharya
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
Dhrumil Panchal
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
Harendra Singh
 
structure and union
structure and unionstructure and union
structure and unionstudent
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
C functions
C functionsC functions

What's hot (20)

Tokens in C++
Tokens in C++Tokens in C++
Tokens in C++
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Operators in C++
Operators in C++Operators in C++
Operators in C++
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Function overloading and overriding
Function overloading and overridingFunction overloading and overriding
Function overloading and overriding
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
 
Command line arguments
Command line argumentsCommand line arguments
Command line arguments
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Structure in C
Structure in CStructure in C
Structure in C
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 
structure and union
structure and unionstructure and union
structure and union
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
C functions
C functionsC functions
C functions
 

Viewers also liked

friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
Ritika Sharma
 
Lexical analysis
Lexical analysisLexical analysis
Lexical analysis
Richa Sharma
 
Compiler Design Introduction
Compiler Design IntroductionCompiler Design Introduction
Compiler Design Introduction
Richa Sharma
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysis
Richa Sharma
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading Charndeep Sekhon
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.pptTareq Hasan
 
Function Overlaoding
Function OverlaodingFunction Overlaoding
Function Overlaoding
Chandrakiran Satdeve
 
Function overloading
Function overloadingFunction overloading
Function overloading
Jnyanaranjan Dash
 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend class
Abhishek Wadhwa
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
Aabha Tiwari
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingKumar
 
Function overloading in c++
Function overloading in c++Function overloading in c++
Function overloading in c++
Learn By Watch
 
Overloading in java
Overloading in javaOverloading in java
Overloading in java
774474
 
constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programming
Ashita Agrawal
 
Constructor and destructor in c++
Constructor and destructor in c++Constructor and destructor in c++
Constructor and destructor in c++
Learn By Watch
 
Constructor & Destructor
Constructor & DestructorConstructor & Destructor
Constructor & Destructor
KV(AFS) Utarlai, Barmer (Rajasthan)
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
Vinod Kumar
 
operator overloading in c++
operator overloading in c++operator overloading in c++
operator overloading in c++
harman kaur
 
Friends function and_classes
Friends function and_classesFriends function and_classes
Friends function and_classesasadsardar
 

Viewers also liked (20)

friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
Lexical analysis
Lexical analysisLexical analysis
Lexical analysis
 
Compiler Design Introduction
Compiler Design IntroductionCompiler Design Introduction
Compiler Design Introduction
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysis
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
Function Overlaoding
Function OverlaodingFunction Overlaoding
Function Overlaoding
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend class
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Function overloading in c++
Function overloading in c++Function overloading in c++
Function overloading in c++
 
Overloading in java
Overloading in javaOverloading in java
Overloading in java
 
constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programming
 
Constructor and destructor in c++
Constructor and destructor in c++Constructor and destructor in c++
Constructor and destructor in c++
 
Constructor & Destructor
Constructor & DestructorConstructor & Destructor
Constructor & Destructor
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
operator overloading in c++
operator overloading in c++operator overloading in c++
operator overloading in c++
 
Friends function and_classes
Friends function and_classesFriends function and_classes
Friends function and_classes
 

Similar to Function overloading(c++)

Presentation on polymorphism in c++.pptx
Presentation on polymorphism in c++.pptxPresentation on polymorphism in c++.pptx
Presentation on polymorphism in c++.pptx
vishwadeep15
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
SangeetaBorde3
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 FunctionDeepak Singh
 
46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1
AmIt Prasad
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptx
hara69
 
3 Function Overloading
3 Function Overloading3 Function Overloading
3 Function Overloading
Praveen M Jigajinni
 
Data structure scope of variables
Data structure scope of variablesData structure scope of variables
Data structure scope of variablesSaurav Kumar
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
Haresh Jaiswal
 
Function in C++
Function in C++Function in C++
Function in C++
Prof Ansari
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Pranali Chaudhari
 
C,c++ interview q&a
C,c++ interview q&aC,c++ interview q&a
C,c++ interview q&aKumaran K
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
Saranya saran
 
Virtual function
Virtual functionVirtual function
Virtual function
zindadili
 
04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx
Manas40552
 
Functions in c
Functions in cFunctions in c
Functions in c
SunithaVesalpu
 
polymorphism.pdf
polymorphism.pdfpolymorphism.pdf
polymorphism.pdf
riyawagh2
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
UMA PARAMESWARI
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
Sampath Kumar
 

Similar to Function overloading(c++) (20)

Presentation on polymorphism in c++.pptx
Presentation on polymorphism in c++.pptxPresentation on polymorphism in c++.pptx
Presentation on polymorphism in c++.pptx
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
 
46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptx
 
3 Function Overloading
3 Function Overloading3 Function Overloading
3 Function Overloading
 
Data structure scope of variables
Data structure scope of variablesData structure scope of variables
Data structure scope of variables
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Function in C++
Function in C++Function in C++
Function in C++
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
C,c++ interview q&a
C,c++ interview q&aC,c++ interview q&a
C,c++ interview q&a
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Virtual function
Virtual functionVirtual function
Virtual function
 
Ch4 functions
Ch4 functionsCh4 functions
Ch4 functions
 
04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx
 
Functions in c
Functions in cFunctions in c
Functions in c
 
polymorphism.pdf
polymorphism.pdfpolymorphism.pdf
polymorphism.pdf
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 

Recently uploaded

AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
Kamal Acharya
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
Pipe Restoration Solutions
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
Kamal Acharya
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
ankuprajapati0525
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
ssuser9bd3ba
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
ShahidSultan24
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
AafreenAbuthahir2
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
PrashantGoswami42
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 

Recently uploaded (20)

AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 

Function overloading(c++)

  • 2. Polymorphism The word polymorphism is derived from Greek word Poly which means many and morphos which means forms. Polymorphism can be defined as the ability to use the same name for two or more related but technically different tasks. Eg-woman plays role of daughter,sister,wife,mother etc. 2 Ritika sharma
  • 3. Overloading in C++ What is overloading – Overloading means assigning multiple meanings to a function name or operator symbol – It allows multiple definitions of a function with the same name, but different signatures. C++ supports – Function overloading – Operator overloading 3 Ritika sharma
  • 4. Why is Overloading Useful?  Function overloading allows functions that conceptually perform the same task on objects of different types to be given the same name.  Operator overloading provides a convenient notation for manipulating user-defined objects with conventional operators. 4 Ritika sharma
  • 5. Function Overloading Is the process of using the same name for two or more functions Requires each redefinition of a function to use a different function signature that is: different types of parameters, or sequence of parameters, or number of parameters Is used so that a programmer does not have to remember multiple function names 5 Ritika sharma
  • 6. Function Overloading Two or more functions can have the same name but different parameters Example: int max(int a, int b) { if (a>= b) return a; else return b; } float max(float a, float b) { if (a>= b) return a; else return b; } 6 Ritika sharma
  • 7. Overloading Function Call Resolution  Overloaded function call resolution is done by compiler during compilation – The function signature determines which definition is used  a Function signature consists of: – Parameter types and number of parameters supplied to a function  a Function return type is not part of function signature and is not used in function call resolution 7 Ritika sharma
  • 8. void sum(int,int); void sum(double,double); void sum(char,char); void main() { int a=10,b=20 ; double c=7.52,d=8.14; char e=‘a’ , f=‘b’ ; sum(a,b); //calls sum(int x,int y) sum(c,d); //calls sum (double x,double y) sum(e,f); // calls sum(char x,char y) } void sum(int x,int y) { vout<<“n sum of integers are”<<x+y; } void sum(double x,double y) { cout<<“n sum of two floating no are”<<x+y; } void sum(char x,char y) { cout<<“n sum of characters are”<<x+y; 8 Ritika sharma
  • 9. Output: Sum of integers 30 sum of two floating no are 15.66 sum of characters are 195 9 Ritika sharma
  • 10. Void area(int) Void area(int,int); Void area(int,int,int); Int main() { Int side=10,le=5,br=6,a=4,b=5,c=6; Area(side); Area(le,br); Area(a,b,c); Getch(); Return 0; } Void area(int x) { cout<<“area is”<<x*x; } Void area(int x,int y) {cout<<“area of rectang;e”=<<x*y; } Void area(int x,int y,int z) {cout<<“volume is”<<x*y*z; } 10 Ritika sharma
  • 11. Function Selection Involves following Steps. Compiler first tries to find the Exact match in which the type of argument are the same,and uses that func. If an exact match is not found,the compiler user the integral promotions to the actual argument such as,char to int, float to double. When either of them fails ,build in conversions are used(implicit conversion) to the actual arguments and then uses the function whose match is unique.but if there are multiple matches,then compiler will generate an error message. 11 Ritika sharma
  • 12. For ex: long square(long n) long square(double x) Now a func. call such as square(10) will cause an error because int argument can be converted into long also and double also.so it will show ambiguity. User defined conversion are followed if all the conversion are failed. 12 Ritika sharma
  • 14. Scope The scope of a variable is the portion of a program where the variable has meaning (where it exists). A global variable has global (unlimited) scope. A local variable’s scope is restricted to the function that declares the variable. A block variable’s scope is restricted to the block in which the variable is declared. 14 Ritika sharma
  • 15. Understanding Scope Some variables can be accessed throughout an entire program, while others can be accessed only in a limited part of the program The scope of a variable defines where it can be accessed in a program To adequately understand scope, you must be able to distinguish between local and global variables 15 Ritika sharma
  • 16. Local variables Parameters and variables declared inside the definition of a function are local. They only exist inside the function body. Once the function returns, the variables no longer exist! That’s fine! We don’t need them anymore! 16 Ritika sharma
  • 17. Block Variables You can also declare variables that exist only within the body of a compound statement (a block): { int foo; … … } 17 Ritika sharma
  • 18. Global variables You can declare variables outside of any function definition – these variables are global variables. Any function can access/change global variables. Example: flag that indicates whether debugging information should be printed. 18 Ritika sharma
  • 19. Distinguishing Between Local and Global Variables Celebrity names are global because they are known to people everywhere and always refer to those same celebrities Global variables are those that are known to all functions in a program Some named objects in your life are local You might have a local co-worker whose name takes precedence over, or overrides, a global one 19 Ritika sharma
  • 20. A note about Global vs. File scope A variable declared outside of a function is available everywhere, but only the functions that follow it in the file know about it. The book talks about file scope, I’m calling it global scope. 20 Ritika sharma
  • 21. Block Scope int main(void) { int y; { int a = y; cout << a << endl; } cout << a << endl; } Error – a doesn’t exist outside the block! 21 Ritika sharma

Editor's Notes

  1. Multiple function with same name and same number of parameters differ only in data types