SlideShare a Scribd company logo
3
Most read
5
Most read
6
Most read
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

PPT
C++ classes tutorials
PPTX
Inline function
PPTX
Functions in c++
PDF
Function overloading ppt
PPT
Operator Overloading
PPTX
Constructors and destructors
PPTX
classes and objects in C++
PPTX
Polymorphism In c++
C++ classes tutorials
Inline function
Functions in c++
Function overloading ppt
Operator Overloading
Constructors and destructors
classes and objects in C++
Polymorphism In c++

What's hot (20)

PPTX
Inheritance in c++
PPTX
Java package
PPTX
07. Virtual Functions
PDF
C++ OOPS Concept
PPTX
Structure in C
PPTX
Polymorphism in c++(ppt)
PPTX
Inheritance in java
PPTX
Constructor and Types of Constructors
PPTX
Inheritance in java
PPTX
Static Data Members and Member Functions
PPTX
Inheritance in c++
PPTX
INLINE FUNCTION IN C++
PPT
Class and object in C++
PPTX
[OOP - Lec 19] Static Member Functions
PPTX
Pointer in c
PPTX
Functions in python
PPT
File handling in c
PPSX
INTRODUCTION TO C PROGRAMMING
PPTX
MULTI THREADING IN JAVA
PDF
Managing I/O in c++
Inheritance in c++
Java package
07. Virtual Functions
C++ OOPS Concept
Structure in C
Polymorphism in c++(ppt)
Inheritance in java
Constructor and Types of Constructors
Inheritance in java
Static Data Members and Member Functions
Inheritance in c++
INLINE FUNCTION IN C++
Class and object in C++
[OOP - Lec 19] Static Member Functions
Pointer in c
Functions in python
File handling in c
INTRODUCTION TO C PROGRAMMING
MULTI THREADING IN JAVA
Managing I/O in c++
Ad

Viewers also liked (20)

PPT
friend function(c++)
PDF
Lexical analysis
PDF
Compiler Design Introduction
PPTX
Compiler design syntax analysis
PPTX
Function overloading
PPTX
Presentation on overloading
PPT
08 c++ Operator Overloading.ppt
PPT
Function Overlaoding
PPTX
Function overloading
PPTX
Friend function & friend class
PPTX
OPERATOR OVERLOADING IN C++
PDF
Function overloading
PPTX
Operator overloading
PPTX
Function overloading in c++
PPTX
Function overloading and overriding
PPTX
Overloading in java
PPT
constructor and destructor-object oriented programming
PPTX
Constructor and destructor in c++
PPT
Constructor & Destructor
PPTX
Constructor ppt
friend function(c++)
Lexical analysis
Compiler Design Introduction
Compiler design syntax analysis
Function overloading
Presentation on overloading
08 c++ Operator Overloading.ppt
Function Overlaoding
Function overloading
Friend function & friend class
OPERATOR OVERLOADING IN C++
Function overloading
Operator overloading
Function overloading in c++
Function overloading and overriding
Overloading in java
constructor and destructor-object oriented programming
Constructor and destructor in c++
Constructor & Destructor
Constructor ppt
Ad

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

PPTX
CPP07 - Scope
PPT
Functions in c++
PPTX
Presentation on polymorphism in c++.pptx
PPTX
Functions in C++ programming language.pptx
PPTX
Functions in C++ (OOP)
PDF
how to reuse code
PPTX
Operator Overloading and Scope of Variable
PPTX
functions
PPTX
Silde of the cse fundamentals a deep analysis
PDF
PPTX
Cs1123 8 functions
PPT
PDF
Functionssssssssssssssssssssssssssss.pdf
PPT
C++ Functions.ppt
PPT
power point presentation on object oriented programming functions concepts
PPT
02 functions, variables, basic input and output of c++
PPT
Material 3 (4).ppt this ppt is about the
PPTX
C++_Functions_Detailed_Presentation.pptx
PPTX
Function C++
PPTX
FUNCTIONS, CLASSES AND OBJECTS.pptx
CPP07 - Scope
Functions in c++
Presentation on polymorphism in c++.pptx
Functions in C++ programming language.pptx
Functions in C++ (OOP)
how to reuse code
Operator Overloading and Scope of Variable
functions
Silde of the cse fundamentals a deep analysis
Cs1123 8 functions
Functionssssssssssssssssssssssssssss.pdf
C++ Functions.ppt
power point presentation on object oriented programming functions concepts
02 functions, variables, basic input and output of c++
Material 3 (4).ppt this ppt is about the
C++_Functions_Detailed_Presentation.pptx
Function C++
FUNCTIONS, CLASSES AND OBJECTS.pptx

Recently uploaded (20)

PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
Welding lecture in detail for understanding
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
DOCX
573137875-Attendance-Management-System-original
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
composite construction of structures.pdf
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
web development for engineering and engineering
PPTX
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
PDF
Well-logging-methods_new................
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Welding lecture in detail for understanding
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
573137875-Attendance-Management-System-original
Lesson 3_Tessellation.pptx finite Mathematics
Embodied AI: Ushering in the Next Era of Intelligent Systems
composite construction of structures.pdf
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
CH1 Production IntroductoryConcepts.pptx
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
web development for engineering and engineering
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
Well-logging-methods_new................
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx

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

  • #9: Multiple function with same name and same number of parameters differ only in data types