SlideShare una empresa de Scribd logo
1 de 21
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

Más contenido relacionado

La actualidad más candente

仕事で使うF#
仕事で使うF#仕事で使うF#
仕事で使うF#
bleis tift
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
rohassanie
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6
rohassanie
 

La actualidad más candente (20)

pointer, virtual function and polymorphism
pointer, virtual function and polymorphismpointer, virtual function and polymorphism
pointer, virtual function and polymorphism
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)
 
This pointer
This pointerThis pointer
This pointer
 
仕事で使うF#
仕事で使うF#仕事で使うF#
仕事で使うF#
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
 
Function in c
Function in cFunction in c
Function in c
 
Function in c
Function in cFunction in c
Function in c
 
C++ theory
C++ theoryC++ theory
C++ theory
 
lets play with "c"..!!! :):)
lets play with "c"..!!! :):)lets play with "c"..!!! :):)
lets play with "c"..!!! :):)
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 
Lecture 9- Control Structures 1
Lecture 9- Control Structures 1Lecture 9- Control Structures 1
Lecture 9- Control Structures 1
 
C++ book
C++ bookC++ book
C++ book
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6
 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - Reference
 
Functions in C
Functions in CFunctions in C
Functions in C
 
C++ programming function
C++ programming functionC++ programming function
C++ programming function
 
Recursion in c
Recursion in cRecursion in c
Recursion in c
 
Advanced C programming
Advanced C programmingAdvanced C programming
Advanced C programming
 

Destacado

нові надходження червня2015
нові надходження червня2015нові надходження червня2015
нові надходження червня2015
Maryna Zaharova
 
ยินดีกับทุกท่านนะคะ
ยินดีกับทุกท่านนะคะยินดีกับทุกท่านนะคะ
ยินดีกับทุกท่านนะคะ
Maruko Supertinger
 
Current Projects
Current ProjectsCurrent Projects
Current Projects
Frank Jing
 
04.การกระจายเชื้อเพลิงในต่างประเทศ
04.การกระจายเชื้อเพลิงในต่างประเทศ04.การกระจายเชื้อเพลิงในต่างประเทศ
04.การกระจายเชื้อเพลิงในต่างประเทศ
Kobwit Piriyawat
 

Destacado (20)

нові надходження червня2015
нові надходження червня2015нові надходження червня2015
нові надходження червня2015
 
easy Vocabulary
easy Vocabulary easy Vocabulary
easy Vocabulary
 
职业规划
职业规划职业规划
职业规划
 
RECTAS PARALELAS Y PERPENDICULARES
RECTAS PARALELAS Y PERPENDICULARESRECTAS PARALELAS Y PERPENDICULARES
RECTAS PARALELAS Y PERPENDICULARES
 
Pedagogia progresista
Pedagogia progresistaPedagogia progresista
Pedagogia progresista
 
Tecnologia eduativa
Tecnologia eduativaTecnologia eduativa
Tecnologia eduativa
 
Retail Idea
Retail IdeaRetail Idea
Retail Idea
 
Proyecto cine
Proyecto cineProyecto cine
Proyecto cine
 
CAP and BASE
CAP and BASECAP and BASE
CAP and BASE
 
Decimales: Valor Posicional
Decimales: Valor PosicionalDecimales: Valor Posicional
Decimales: Valor Posicional
 
DevOps Boston - Heartbleed at Acquia
DevOps Boston - Heartbleed at AcquiaDevOps Boston - Heartbleed at Acquia
DevOps Boston - Heartbleed at Acquia
 
Keynote "Kommunikation im Gesundheitssektor" vom 25. April 2013, Wien
Keynote "Kommunikation im Gesundheitssektor" vom 25. April 2013, WienKeynote "Kommunikation im Gesundheitssektor" vom 25. April 2013, Wien
Keynote "Kommunikation im Gesundheitssektor" vom 25. April 2013, Wien
 
ยินดีกับทุกท่านนะคะ
ยินดีกับทุกท่านนะคะยินดีกับทุกท่านนะคะ
ยินดีกับทุกท่านนะคะ
 
Practica normalizacion
Practica normalizacionPractica normalizacion
Practica normalizacion
 
Current Projects
Current ProjectsCurrent Projects
Current Projects
 
04.การกระจายเชื้อเพลิงในต่างประเทศ
04.การกระจายเชื้อเพลิงในต่างประเทศ04.การกระจายเชื้อเพลิงในต่างประเทศ
04.การกระจายเชื้อเพลิงในต่างประเทศ
 
2б космос
2б космос2б космос
2б космос
 
11.ลม
11.ลม11.ลม
11.ลม
 
Curso deferias
Curso deferiasCurso deferias
Curso deferias
 
Webwriting That Works
Webwriting That WorksWebwriting That Works
Webwriting That Works
 

Similar a Function oveloading

Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
Deepak Singh
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptx
hara69
 
Data structure scope of variables
Data structure scope of variablesData structure scope of variables
Data structure scope of variables
Saurav Kumar
 
C,c++ interview q&a
C,c++ interview q&aC,c++ interview q&a
C,c++ interview q&a
Kumaran K
 

Similar a Function oveloading (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
 
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
 
chapter-8-function-overloading.pdf
chapter-8-function-overloading.pdfchapter-8-function-overloading.pdf
chapter-8-function-overloading.pdf
 

Último

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 

Último (20)

Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 

Function oveloading

  • 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

Notas del editor

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