SlideShare ist ein Scribd-Unternehmen logo
1 von 18
Downloaden Sie, um offline zu lesen
FUNCTION OVERLOADING
Subject: Programming in C++
By Jasleen Kaur
Assistant Professor (CSE)
Function Overloading
• If any class have multiple functions with same
names but different parameters then they are said
to be overloaded. Function overloading allows you
to use the same name for different functions, to
perform, either same or different functions in the
same class.
• If you have to perform one single operation but with
different number or types of arguments, then you
can simply overload the function.
Way to Overload a Function
• By changing number of Arguments.
• By having different datatypes of argument.
• When an overloaded function is called, the
C++ compiler selects the proper function by
examining the number, types and order of
arguments in the function call.
• Function overloading is commonly used to
create several functions of same name that
perform similar tasks but on data of different
types.
Number of Arguments different
• In this, we can 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
int sum (int x, int y)
{
cout << x+y;
}
Here sum() function is overloaded, to have two and three
arguments. Which sum() function will be called, depends on the
number of arguments.
int sum(int x, int y, int z)
{
cout << x+y+z;
}
Different Datatype 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 this
program, we have two sum() function, first one
gets two integer arguments and second one
gets two double arguments.
Example
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);
}
WAP to overload square function which
calculate square of an int and square of
double.
Question for Practice
#include<iostream.h>
#include<conio.h>
int square(int x)
{
cout<<“square of integer”<<x<<“is”;
return x*x;
}
double square(double y)
{
cout<<“square of double”<<y<<“is”;
return y*y;
}
void main()
{
cout<<square(7);//calls int version
cout<<endl;
cout<<square(7.5); //calls double version
cout<<endl;
getch();
}
How Compiler differentiates
overloaded functions?
• Overloaded functions are distinguished by
their signatures.
• A signature is a combination of function’s
name and its parameter types(in order).
• Compiler encodes each function identifier
with the number and types of its parameters.
This is sometimes referred to as name
mangling.
• The compiler uses only parameter list to
distinguish between functions of same name.
How Compiler differentiates
overloaded functions?
void f(int x);
void f(int &x); // error
Two functions cannot be overloaded when the
only difference is that one takes a reference
parameter and the other takes a normal, call-by-
value parameter.
Functions can be distinguished on the basis of return type,
they cannot be overloaded on this basis.
Function with 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.
• Example
sum(int x,int y=0)
{
cout << x+y;
}
Cont..
• Here we have provided a default value for y, during function
definition.
• Example
int main( )
{
sum(10);
sum(10,0);
sum(10,10);
}
• 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.
Rules for using Default Arguments
• Only the last argument must be given default
value. You cannot have a default argument
followed by non-default argument.
• sum (int x, int y);
• sum (int x, int y=0);
• sum (int x=0, int y); // This is Incorrect
Rules for using Default Arguments
• If you default an argument, then you will have
to default all the subsequent arguments after
that.
– sum (int x,int y=0);
– sum (int x,int y=0,int z); // This is incorrect
– sum (int x,int y=10,int z=10); // Correct
• You can give any value a default value to
argument, compatible with its datatype.

Weitere ähnliche Inhalte

Was ist angesagt? (20)

structure and union
structure and unionstructure and union
structure and union
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Function in C
Function in CFunction in C
Function in C
 
User defined functions in C programmig
User defined functions in C programmigUser defined functions in C programmig
User defined functions in C programmig
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 
Enums in c
Enums in cEnums in c
Enums in c
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Strings
StringsStrings
Strings
 
Pointer in c program
Pointer in c programPointer in c program
Pointer in c program
 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
 
Function overloading and overriding
Function overloading and overridingFunction overloading and overriding
Function overloading and overriding
 
POINTERS IN C
POINTERS IN CPOINTERS IN C
POINTERS IN C
 
Function Pointer
Function PointerFunction Pointer
Function Pointer
 
C functions
C functionsC functions
C functions
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
This pointer
This pointerThis pointer
This pointer
 

Andere mochten auch

Function overloading in c++
Function overloading in c++Function overloading in c++
Function overloading in c++Learn By Watch
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading Charndeep Sekhon
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingKumar
 
DATATYPE IN C# CSHARP.net
DATATYPE IN C# CSHARP.netDATATYPE IN C# CSHARP.net
DATATYPE IN C# CSHARP.netSireesh K
 
C++ overloading
C++ overloadingC++ overloading
C++ overloadingsanya6900
 
Vb.net session 01
Vb.net session 01Vb.net session 01
Vb.net session 01Niit Care
 
Operator overloadng
Operator overloadngOperator overloadng
Operator overloadngpreethalal
 
Control Structures in Visual Basic
Control Structures in  Visual BasicControl Structures in  Visual Basic
Control Structures in Visual BasicTushar Jain
 
Introduction to ADO.NET
Introduction to ADO.NETIntroduction to ADO.NET
Introduction to ADO.NETrchakra
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default argumentsNikhil Pandit
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++LPU
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++Learn By Watch
 

Andere mochten auch (20)

Function overloading
Function overloadingFunction overloading
Function overloading
 
Function overloading in c++
Function overloading in c++Function overloading in c++
Function overloading in c++
 
Function Overlaoding
Function OverlaodingFunction Overlaoding
Function Overlaoding
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading
 
functions of C++
functions of C++functions of C++
functions of C++
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
DATATYPE IN C# CSHARP.net
DATATYPE IN C# CSHARP.netDATATYPE IN C# CSHARP.net
DATATYPE IN C# CSHARP.net
 
7.data types in c#
7.data types in c#7.data types in c#
7.data types in c#
 
C++ overloading
C++ overloadingC++ overloading
C++ overloading
 
3 Function Overloading
3 Function Overloading3 Function Overloading
3 Function Overloading
 
Vb.net session 01
Vb.net session 01Vb.net session 01
Vb.net session 01
 
Operator overloadng
Operator overloadngOperator overloadng
Operator overloadng
 
Data types
Data typesData types
Data types
 
Conditional statements
Conditional statementsConditional statements
Conditional statements
 
C#.NET
C#.NETC#.NET
C#.NET
 
Control Structures in Visual Basic
Control Structures in  Visual BasicControl Structures in  Visual Basic
Control Structures in Visual Basic
 
Introduction to ADO.NET
Introduction to ADO.NETIntroduction to ADO.NET
Introduction to ADO.NET
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++
 

Ähnlich wie 03 function overloading (20)

Functions1
Functions1Functions1
Functions1
 
3d7b7 session4 c++
3d7b7 session4 c++3d7b7 session4 c++
3d7b7 session4 c++
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
06
0606
06
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Review functions
Review functionsReview functions
Review functions
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Functions
FunctionsFunctions
Functions
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
overloading in C++
overloading in C++overloading in C++
overloading in C++
 
Pro
ProPro
Pro
 
Reference Parameter, Passing object by reference, constant parameter & Defaul...
Reference Parameter, Passing object by reference, constant parameter & Defaul...Reference Parameter, Passing object by reference, constant parameter & Defaul...
Reference Parameter, Passing object by reference, constant parameter & Defaul...
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloading
 
Functions
FunctionsFunctions
Functions
 
85ec7 session2 c++
85ec7 session2 c++85ec7 session2 c++
85ec7 session2 c++
 
Functions in C++.pdf
Functions in C++.pdfFunctions in C++.pdf
Functions in C++.pdf
 

Mehr von Jasleen Kaur (Chandigarh University)

Priority Based Congestion Avoidance Hybrid Scheme published in IEEE
Priority Based Congestion Avoidance Hybrid Scheme published in IEEE Priority Based Congestion Avoidance Hybrid Scheme published in IEEE
Priority Based Congestion Avoidance Hybrid Scheme published in IEEE Jasleen Kaur (Chandigarh University)
 

Mehr von Jasleen Kaur (Chandigarh University) (19)

Graphs data structures
Graphs data structuresGraphs data structures
Graphs data structures
 
B+ trees and height balance tree
B+ trees and height balance treeB+ trees and height balance tree
B+ trees and height balance tree
 
Basics of c++
Basics of c++Basics of c++
Basics of c++
 
Static variables
Static variablesStatic variables
Static variables
 
Operating system notes pdf
Operating system notes pdfOperating system notes pdf
Operating system notes pdf
 
Priority Based Congestion Avoidance Hybrid Scheme published in IEEE
Priority Based Congestion Avoidance Hybrid Scheme published in IEEE Priority Based Congestion Avoidance Hybrid Scheme published in IEEE
Priority Based Congestion Avoidance Hybrid Scheme published in IEEE
 
Chapter 2.datatypes and operators
Chapter 2.datatypes and operatorsChapter 2.datatypes and operators
Chapter 2.datatypes and operators
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Remote desktop connection
Remote desktop connectionRemote desktop connection
Remote desktop connection
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
Calculating garbage value in case of overflow
Calculating garbage value in case of overflowCalculating garbage value in case of overflow
Calculating garbage value in case of overflow
 
Final jasleen ppt
Final jasleen pptFinal jasleen ppt
Final jasleen ppt
 
License Plate recognition
License Plate recognitionLicense Plate recognition
License Plate recognition
 
The solar system
The solar system The solar system
The solar system
 
The solar system
The solar systemThe solar system
The solar system
 
Afforestation environmental issue
Afforestation environmental issueAfforestation environmental issue
Afforestation environmental issue
 
Data aggregation in wireless sensor networks
Data aggregation in wireless sensor networksData aggregation in wireless sensor networks
Data aggregation in wireless sensor networks
 
Network security
Network securityNetwork security
Network security
 

Kürzlich hochgeladen

How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfLivetecs LLC
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Best Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfBest Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfIdiosysTechnologies1
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 

Kürzlich hochgeladen (20)

How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdf
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Advantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your BusinessAdvantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your Business
 
Best Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfBest Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdf
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 

03 function overloading

  • 1. FUNCTION OVERLOADING Subject: Programming in C++ By Jasleen Kaur Assistant Professor (CSE)
  • 2. Function Overloading • If any class have multiple functions with same names but different parameters then they are said to be overloaded. Function overloading allows you to use the same name for different functions, to perform, either same or different functions in the same class. • If you have to perform one single operation but with different number or types of arguments, then you can simply overload the function.
  • 3. Way to Overload a Function • By changing number of Arguments. • By having different datatypes of argument.
  • 4. • When an overloaded function is called, the C++ compiler selects the proper function by examining the number, types and order of arguments in the function call. • Function overloading is commonly used to create several functions of same name that perform similar tasks but on data of different types.
  • 5. Number of Arguments different • In this, we can 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 int sum (int x, int y) { cout << x+y; } Here sum() function is overloaded, to have two and three arguments. Which sum() function will be called, depends on the number of arguments. int sum(int x, int y, int z) { cout << x+y+z; }
  • 6. Different Datatype 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 this program, we have two sum() function, first one gets two integer arguments and second one gets two double arguments.
  • 7. Example 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); }
  • 8. WAP to overload square function which calculate square of an int and square of double. Question for Practice
  • 9. #include<iostream.h> #include<conio.h> int square(int x) { cout<<“square of integer”<<x<<“is”; return x*x; } double square(double y) { cout<<“square of double”<<y<<“is”; return y*y; }
  • 10. void main() { cout<<square(7);//calls int version cout<<endl; cout<<square(7.5); //calls double version cout<<endl; getch(); }
  • 11. How Compiler differentiates overloaded functions? • Overloaded functions are distinguished by their signatures. • A signature is a combination of function’s name and its parameter types(in order). • Compiler encodes each function identifier with the number and types of its parameters. This is sometimes referred to as name mangling.
  • 12. • The compiler uses only parameter list to distinguish between functions of same name. How Compiler differentiates overloaded functions?
  • 13. void f(int x); void f(int &x); // error Two functions cannot be overloaded when the only difference is that one takes a reference parameter and the other takes a normal, call-by- value parameter.
  • 14. Functions can be distinguished on the basis of return type, they cannot be overloaded on this basis.
  • 15. Function with 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. • Example sum(int x,int y=0) { cout << x+y; }
  • 16. Cont.. • Here we have provided a default value for y, during function definition. • Example int main( ) { sum(10); sum(10,0); sum(10,10); } • 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.
  • 17. Rules for using Default Arguments • Only the last argument must be given default value. You cannot have a default argument followed by non-default argument. • sum (int x, int y); • sum (int x, int y=0); • sum (int x=0, int y); // This is Incorrect
  • 18. Rules for using Default Arguments • If you default an argument, then you will have to default all the subsequent arguments after that. – sum (int x,int y=0); – sum (int x,int y=0,int z); // This is incorrect – sum (int x,int y=10,int z=10); // Correct • You can give any value a default value to argument, compatible with its datatype.