SlideShare a Scribd company logo
1 of 25
In C++, two or more functions can share
the same name as long as their parameter
        declarations are different.

 In this situation, the functions that share
the same name are said to be overloaded,
 and the process is referred to as function
                 overloading
To see why function overloading is important,
first consider three functions defined by the
C subset: abs(), labs(), and fabs().

abs() returns the absolute value of an integer,

labs() returns the absolute value of a long,
and

fabs() returns the absolute value of a double.
Although these functions perform almost
identical actions, in C three slightly
different names must be used to represent
these essentially similar tasks.

Example: Function Overloading
Function Overloading – DEFINITION

It is the process of using the same name for
two or more functions.

The secret to overloading is that each
redefinition of the function must use
either-
     • different types of parameters
     • different number of parameters.
The key to function overloading is a function’s
                    argument list.

 A function’s argument list is known as its signature.

                        Example :

Different types of       Different number of parameters
arguments
void print(int a);     void area(float r);
void print (double b); void area(float l, float b);
void print(char c);    void area(float a, float b, float c);
If two function have same number and
types of arguments in the same order, they
are said to have the same signature.

There is no importance to their variable
names.
    • void print(int x, float y);
    • void print(int p, float q); are said to
have same signature.
Calling Overloaded Function

 Overloaded functions are called just like
          ordinary functions.

 The signature determines which function
among the overloaded functions should be
                executed.
                 print(3);
                 print(‘B’);
STEPS FOR FINDING BEST MATCH

The actual arguments are compared with
  formal arguments of the overloaded
 functions and the best match is found
       through a number of steps.

  In order to find the best match, the
compiler follows the steps given below:
STEPS - 1 : Search for an exact match

    If the type of the actual and formal
argument is exactly the same, the compiler
            invokes that function.
      void print(int x); // function #1
      void print(float p); // function #2
      void print(char c); // function #3
                   ------------
                   -------------
   print(5.3); //invokes the function #2
STEPS - 2 : A match through promotion

If no exact match is found, an attempt is
     made to achieve a match through
                 promotion.
     void print(int x); // function #1
     void print(float p); // function #2
    void print(double f); // function #3
                  ------------
                  -------------
   print(‘c’); //matches the function #1
             through promotion
STEPS - 3 : A match through standard
               C++ conversion rules

If the first and second steps fail, then an
  attempt is made to find a best match
          through conversion rule.
    void print(char x); // function #1
     void print(float p); // function #2
                   ------------
                  -------------
 print(342); //matches the function #2
        int 342 is converted to float
STEPS - 4 : A match through user
          defined conversion

If all the above three steps fail to find a
match, then an attempt is made to find a
     match by applying user defined
             conversion rules.
Constructor Overloading

Just like any other function, constructors
          can also be overloaded.

 We can use constructor overloading for
initializing the objects based on different
                 conditions.
In Constructor Overloading, we need not
 call the constructor separately because
      they are invoked automatically.

                   But

In Function Overloading, we have to invoke
             them separately.
Functions with Default Arguments
                   Vs
              Overloading

 Functions with default arguments can be
called with optional number of arguments
    and hence it gives an appearance of
            function overloading.
  float calc(float p, int q=8, float r=18.3);
                -------------------
              float f = calc(3.5);
            float f = calc(3.5, 10);
It is one type of Static Polymorphism.

C++ has about 45 operators. These operators
 are defined for the fundamental data types
                (int, float etc).

While defining a class, we are actually creating
               a new datatype.

Most of the C++ operators can be overloaded
    to apply to our new class datatype.
Operator overloading is the
concept of giving new meaning to
    an existing C++ operator.
• When overloaded, the operators are
  implemented as functions using the operator
  keyword.

• For example, the syntax of overloading the
  addition operator “+” would be operator+().
One of the nice features of C++ is
that you can give special meanings
 to operators, when they are used
 with user-defined classes. This is
    called operator overloading.
You can implement C++ operator
     overloads by providing special
member-functions on your classes that
follow a particular naming convention.
     For example, to overload the +
  operator for your class, you would
  provide a member-function named
        operator+ on your class.
1. Read the following function definition :
void print(int x)
{
    cout << x;
}
void print (float x)
 {
    cout << x;
}
void print(char x)
 {
    cout << x;
}
a. All these functions can be used in the same
   program. Explain this feature of C++
b. Write sample statements to invoke each of
   these functions.

a) About FUNCTION OVERLOADING

b) print(35); print(32.7); print(‘M’);
2. Function with default arguments performs
   overloading. Explain this fact with the help
   of example.

• Functions with default arguments can be
  called with optional number of arguments
  and hence it gives an appearance of function
  overloading.
  float calc(float p, int q=8, float r=18.3);
  -------------------
  float f = calc(3.5);
  float f = calc(3.5, 10);
3. Why is function overloading connected to
   compile-time polymorphism or early
   binding?

• Early binding refers to the ability of the
  compiler to relate or bind a function call with
  the function definition during compilation
  itself. FUNCTION OVERLOADING comes under
  this category because it match the actual
  arguments with formal arguments during
  compile-time.

More Related Content

What's hot

Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructorsNilesh Dalvi
 
Advanced Programming C++
Advanced Programming C++Advanced Programming C++
Advanced Programming C++guestf0562b
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingNilesh Dalvi
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++Vraj Patel
 
sSCOPE RESOLUTION OPERATOR.pptx
sSCOPE RESOLUTION OPERATOR.pptxsSCOPE RESOLUTION OPERATOR.pptx
sSCOPE RESOLUTION OPERATOR.pptxNidhi Mehra
 
Function in c program
Function in c programFunction in c program
Function in c programumesh patil
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default argumentsNikhil Pandit
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingBurhan Ahmed
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in clavanya marichamy
 
Types of Constructor in C++
Types of Constructor in C++Types of Constructor in C++
Types of Constructor in C++Bhavik Vashi
 
standard template library(STL) in C++
standard template library(STL) in C++standard template library(STL) in C++
standard template library(STL) in C++•sreejith •sree
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ pptKumar
 
class and objects
class and objectsclass and objects
class and objectsPayel Guria
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statementnarmadhakin
 

What's hot (20)

Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Advanced Programming C++
Advanced Programming C++Advanced Programming C++
Advanced Programming C++
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
 
sSCOPE RESOLUTION OPERATOR.pptx
sSCOPE RESOLUTION OPERATOR.pptxsSCOPE RESOLUTION OPERATOR.pptx
sSCOPE RESOLUTION OPERATOR.pptx
 
C functions
C functionsC functions
C functions
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
 
Types of Constructor in C++
Types of Constructor in C++Types of Constructor in C++
Types of Constructor in C++
 
standard template library(STL) in C++
standard template library(STL) in C++standard template library(STL) in C++
standard template library(STL) in C++
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ ppt
 
class and objects
class and objectsclass and objects
class and objects
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 

Similar to Function overloading

OODP UNIT 2 Function overloading
OODP UNIT 2 Function overloadingOODP UNIT 2 Function overloading
OODP UNIT 2 Function overloadingShanmuganathan C
 
Presentation on polymorphism in c++.pptx
Presentation on polymorphism in c++.pptxPresentation on polymorphism in c++.pptx
Presentation on polymorphism in c++.pptxvishwadeep15
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programmingAMAN ANAND
 
Functions in C++
Functions in C++Functions in C++
Functions in C++home
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloadingankush_kumar
 
Amit user defined functions xi (2)
Amit  user defined functions xi (2)Amit  user defined functions xi (2)
Amit user defined functions xi (2)Arpit Meena
 
Functions in c language
Functions in c language Functions in c language
Functions in c language tanmaymodi4
 
Functions in c language
Functions in c languageFunctions in c language
Functions in c languageTanmay Modi
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxvekariyakashyap
 
overloading in C++
overloading in C++overloading in C++
overloading in C++Prof Ansari
 

Similar to Function overloading (20)

3 Function Overloading
3 Function Overloading3 Function Overloading
3 Function Overloading
 
OODP UNIT 2 Function overloading
OODP UNIT 2 Function overloadingOODP UNIT 2 Function overloading
OODP UNIT 2 Function overloading
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Presentation on polymorphism in c++.pptx
Presentation on polymorphism in c++.pptxPresentation on polymorphism in c++.pptx
Presentation on polymorphism in c++.pptx
 
[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloading
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Amit user defined functions xi (2)
Amit  user defined functions xi (2)Amit  user defined functions xi (2)
Amit user defined functions xi (2)
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Functions in c language
Functions in c languageFunctions in c language
Functions in c language
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
 
Notes on c++
Notes on c++Notes on c++
Notes on c++
 
Functions
FunctionsFunctions
Functions
 
Function in C++
Function in C++Function in C++
Function in C++
 
overloading in C++
overloading in C++overloading in C++
overloading in C++
 

More from Selvin Josy Bai Somu (8)

Client sidescripting javascript
Client sidescripting javascriptClient sidescripting javascript
Client sidescripting javascript
 
Web technology
Web technologyWeb technology
Web technology
 
Data structure stack&queue basics
Data structure stack&queue   basicsData structure stack&queue   basics
Data structure stack&queue basics
 
Files in c++
Files in c++Files in c++
Files in c++
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
 
Inheritance
InheritanceInheritance
Inheritance
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Basics of c++
Basics of c++Basics of c++
Basics of c++
 

Function overloading

  • 1.
  • 2. In C++, two or more functions can share the same name as long as their parameter declarations are different. In this situation, the functions that share the same name are said to be overloaded, and the process is referred to as function overloading
  • 3. To see why function overloading is important, first consider three functions defined by the C subset: abs(), labs(), and fabs(). abs() returns the absolute value of an integer, labs() returns the absolute value of a long, and fabs() returns the absolute value of a double.
  • 4. Although these functions perform almost identical actions, in C three slightly different names must be used to represent these essentially similar tasks. Example: Function Overloading
  • 5. Function Overloading – DEFINITION It is the process of using the same name for two or more functions. The secret to overloading is that each redefinition of the function must use either- • different types of parameters • different number of parameters.
  • 6. The key to function overloading is a function’s argument list. A function’s argument list is known as its signature. Example : Different types of Different number of parameters arguments void print(int a); void area(float r); void print (double b); void area(float l, float b); void print(char c); void area(float a, float b, float c);
  • 7. If two function have same number and types of arguments in the same order, they are said to have the same signature. There is no importance to their variable names. • void print(int x, float y); • void print(int p, float q); are said to have same signature.
  • 8. Calling Overloaded Function Overloaded functions are called just like ordinary functions. The signature determines which function among the overloaded functions should be executed. print(3); print(‘B’);
  • 9. STEPS FOR FINDING BEST MATCH The actual arguments are compared with formal arguments of the overloaded functions and the best match is found through a number of steps. In order to find the best match, the compiler follows the steps given below:
  • 10. STEPS - 1 : Search for an exact match If the type of the actual and formal argument is exactly the same, the compiler invokes that function. void print(int x); // function #1 void print(float p); // function #2 void print(char c); // function #3 ------------ ------------- print(5.3); //invokes the function #2
  • 11. STEPS - 2 : A match through promotion If no exact match is found, an attempt is made to achieve a match through promotion. void print(int x); // function #1 void print(float p); // function #2 void print(double f); // function #3 ------------ ------------- print(‘c’); //matches the function #1 through promotion
  • 12. STEPS - 3 : A match through standard C++ conversion rules If the first and second steps fail, then an attempt is made to find a best match through conversion rule. void print(char x); // function #1 void print(float p); // function #2 ------------ ------------- print(342); //matches the function #2 int 342 is converted to float
  • 13. STEPS - 4 : A match through user defined conversion If all the above three steps fail to find a match, then an attempt is made to find a match by applying user defined conversion rules.
  • 14. Constructor Overloading Just like any other function, constructors can also be overloaded. We can use constructor overloading for initializing the objects based on different conditions.
  • 15. In Constructor Overloading, we need not call the constructor separately because they are invoked automatically. But In Function Overloading, we have to invoke them separately.
  • 16. Functions with Default Arguments Vs Overloading Functions with default arguments can be called with optional number of arguments and hence it gives an appearance of function overloading. float calc(float p, int q=8, float r=18.3); ------------------- float f = calc(3.5); float f = calc(3.5, 10);
  • 17.
  • 18. It is one type of Static Polymorphism. C++ has about 45 operators. These operators are defined for the fundamental data types (int, float etc). While defining a class, we are actually creating a new datatype. Most of the C++ operators can be overloaded to apply to our new class datatype.
  • 19. Operator overloading is the concept of giving new meaning to an existing C++ operator. • When overloaded, the operators are implemented as functions using the operator keyword. • For example, the syntax of overloading the addition operator “+” would be operator+().
  • 20. One of the nice features of C++ is that you can give special meanings to operators, when they are used with user-defined classes. This is called operator overloading.
  • 21. You can implement C++ operator overloads by providing special member-functions on your classes that follow a particular naming convention. For example, to overload the + operator for your class, you would provide a member-function named operator+ on your class.
  • 22. 1. Read the following function definition : void print(int x) { cout << x; } void print (float x) { cout << x; } void print(char x) { cout << x; }
  • 23. a. All these functions can be used in the same program. Explain this feature of C++ b. Write sample statements to invoke each of these functions. a) About FUNCTION OVERLOADING b) print(35); print(32.7); print(‘M’);
  • 24. 2. Function with default arguments performs overloading. Explain this fact with the help of example. • Functions with default arguments can be called with optional number of arguments and hence it gives an appearance of function overloading. float calc(float p, int q=8, float r=18.3); ------------------- float f = calc(3.5); float f = calc(3.5, 10);
  • 25. 3. Why is function overloading connected to compile-time polymorphism or early binding? • Early binding refers to the ability of the compiler to relate or bind a function call with the function definition during compilation itself. FUNCTION OVERLOADING comes under this category because it match the actual arguments with formal arguments during compile-time.