SlideShare a Scribd company logo
C++ Function Overloading
• C++ allows you to specify more than one
definition for a function name or an operator
in the same scope, which is called function
overloading and operator overloading
respectively.
• An overloaded declaration is a declaration
that is declared with the same name as a
previously declared declaration in the same
scope, except that both declarations have
different arguments and obviously different
definition (implementation).
• When you call an overloaded function or
operator, the compiler determines the most
appropriate definition to use, by comparing
the argument types you have used to call the
function or operator with the parameter types
specified in the definitions.
• The process of selecting the most appropriate
overloaded function or operator is called
overload resolution.
Function Overloading in C++
• You can have multiple definitions for the same
function name in the same scope.
• The definition of the function must differ from
each other by the types and/or the number of
arguments in the argument list.
• You cannot overload function declarations
that differ only by return type.
• Following is the example where same function
print() is being used to print different data
types
#include <iostream>
using namespace std;
class printData
{
public: void print(int i)
{
cout << "Printing int: " << i << endl;
}
void print(double f)
{
cout << "Printing float: " << f << endl;
}
void print(char* c)
{
cout << "Printing character: " << c << endl;
}
};
int main(void)
{
printData pd;
// Call print to print integer
pd.print(5);
// Call print to print float
pd.print(500.263);
// Call print to print character
pd.print("Hello C++");
return 0;
}
• Printing int: 5
• Printing float: 500.263
• Printing character: Hello C++
#include <iostream>
using namespace std;
void display(int);
void display(float);
void display(int, float);
int main() {
int a = 5;
float b = 5.5;
display(a);
display(b);
display(a, b);
return 0;
}
void display(int var) {
cout << "Integer number: " << var
<< endl;
}
void display(float var) {
cout << "Float number: " << var <<
endl;
}
void display(int var1, float var2) {
cout << "Integer number: " <<
var1;
cout << " and float number:" <<
var2;
}
Output
Integer number: 5
Float number: 5.5
Integer number: 5 and float number: 5.5

More Related Content

PPTX
Function overloading
PPTX
TEMPLATES IN JAVA
PDF
PPTX
Functions and Header files ver very useful
PPTX
Presentation on polymorphism in c++.pptx
PPTX
unit_2.pptx
PPTX
Functions IN CPROGRAMMING OF ENGINEERING.pptx
PPTX
Functions in C++ (OOP)
Function overloading
TEMPLATES IN JAVA
Functions and Header files ver very useful
Presentation on polymorphism in c++.pptx
unit_2.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptx
Functions in C++ (OOP)

Similar to Function Overloading.ppt (20)

PPTX
C++ language
PPTX
C_plus_plus
PPTX
Classes function overloading
PPTX
unit_2 (1).pptx
PPT
PPTX
Presentation 5th
PPTX
21CSC101T best ppt ever OODP UNIT-2.pptx
PPT
C++ functions
PPT
C++ functions
PPTX
Functions in C++ programming language.pptx
PPTX
KMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptx
PPT
16717 functions in C++
 
PPTX
C language updated
PPT
Oops lecture 1
PDF
08 -functions
PPT
C++ Functions.ppt
PPTX
Function Overloading Call by value and call by reference
PPT
Functions in C++
ODP
Ppt of c vs c#
PPTX
Function overloading
C++ language
C_plus_plus
Classes function overloading
unit_2 (1).pptx
Presentation 5th
21CSC101T best ppt ever OODP UNIT-2.pptx
C++ functions
C++ functions
Functions in C++ programming language.pptx
KMK1093 CHAPTER 2.kkkpptx KMK1093 CHAPTER 2.kkkpptx
16717 functions in C++
 
C language updated
Oops lecture 1
08 -functions
C++ Functions.ppt
Function Overloading Call by value and call by reference
Functions in C++
Ppt of c vs c#
Function overloading
Ad

More from nivedita murugan (7)

PPT
HTML_Forms_.ppt
PPT
Types of Memory.ppt
PPT
Number_Systems (2).ppt
PPTX
performance evaluation of parallel processors.pptx
PPT
constructor.ppt
PPT
Performance of processor.ppt
PPTX
Data Representation.pptx
HTML_Forms_.ppt
Types of Memory.ppt
Number_Systems (2).ppt
performance evaluation of parallel processors.pptx
constructor.ppt
Performance of processor.ppt
Data Representation.pptx
Ad

Recently uploaded (20)

PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Lecture Notes Electrical Wiring System Components
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Sustainable Sites - Green Building Construction
DOCX
573137875-Attendance-Management-System-original
PPTX
Welding lecture in detail for understanding
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
R24 SURVEYING LAB MANUAL for civil enggi
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
PPT on Performance Review to get promotions
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Foundation to blockchain - A guide to Blockchain Tech
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Embodied AI: Ushering in the Next Era of Intelligent Systems
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Lecture Notes Electrical Wiring System Components
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Model Code of Practice - Construction Work - 21102022 .pdf
Sustainable Sites - Green Building Construction
573137875-Attendance-Management-System-original
Welding lecture in detail for understanding
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
R24 SURVEYING LAB MANUAL for civil enggi
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPT on Performance Review to get promotions
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
CH1 Production IntroductoryConcepts.pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS

Function Overloading.ppt

  • 2. • C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is called function overloading and operator overloading respectively.
  • 3. • An overloaded declaration is a declaration that is declared with the same name as a previously declared declaration in the same scope, except that both declarations have different arguments and obviously different definition (implementation).
  • 4. • When you call an overloaded function or operator, the compiler determines the most appropriate definition to use, by comparing the argument types you have used to call the function or operator with the parameter types specified in the definitions. • The process of selecting the most appropriate overloaded function or operator is called overload resolution.
  • 5. Function Overloading in C++ • You can have multiple definitions for the same function name in the same scope. • The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. • You cannot overload function declarations that differ only by return type.
  • 6. • Following is the example where same function print() is being used to print different data types
  • 7. #include <iostream> using namespace std; class printData { public: void print(int i) { cout << "Printing int: " << i << endl; }
  • 8. void print(double f) { cout << "Printing float: " << f << endl; } void print(char* c) { cout << "Printing character: " << c << endl; } };
  • 9. int main(void) { printData pd; // Call print to print integer pd.print(5); // Call print to print float pd.print(500.263);
  • 10. // Call print to print character pd.print("Hello C++"); return 0; }
  • 11. • Printing int: 5 • Printing float: 500.263 • Printing character: Hello C++
  • 12. #include <iostream> using namespace std; void display(int); void display(float); void display(int, float); int main() { int a = 5; float b = 5.5; display(a); display(b); display(a, b); return 0; } void display(int var) { cout << "Integer number: " << var << endl; } void display(float var) { cout << "Float number: " << var << endl; } void display(int var1, float var2) { cout << "Integer number: " << var1; cout << " and float number:" << var2; }
  • 13. Output Integer number: 5 Float number: 5.5 Integer number: 5 and float number: 5.5