Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
In C++, two or more functions can share
the same name as long as their parameter
        declarations are different.

 In ...
To see why function overloading is important,
first consider three functions defined by the
C subset: abs(), labs(), and f...
Although these functions perform almost
identical actions, in C three slightly
different names must be used to represent
t...
Function Overloading – DEFINITION

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

The secret to over...
The key to function overloading is a function’s
                    argument list.

 A function’s argument list is known a...
If two function have same number and
types of arguments in the same order, they
are said to have the same signature.

Ther...
Calling Overloaded Function

 Overloaded functions are called just like
          ordinary functions.

 The signature dete...
STEPS FOR FINDING BEST MATCH

The actual arguments are compared with
  formal arguments of the overloaded
 functions and t...
STEPS - 1 : Search for an exact match

    If the type of the actual and formal
argument is exactly the same, the compiler...
STEPS - 2 : A match through promotion

If no exact match is found, an attempt is
     made to achieve a match through
    ...
STEPS - 3 : A match through standard
               C++ conversion rules

If the first and second steps fail, then an
  at...
STEPS - 4 : A match through user
          defined conversion

If all the above three steps fail to find a
match, then an ...
Constructor Overloading

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

 We can use constru...
In Constructor Overloading, we need not
 call the constructor separately because
      they are invoked automatically.

  ...
Functions with Default Arguments
                   Vs
              Overloading

 Functions with default arguments can be...
It is one type of Static Polymorphism.

C++ has about 45 operators. These operators
 are defined for the fundamental data ...
Operator overloading is the
concept of giving new meaning to
    an existing C++ operator.
• When overloaded, the operator...
One of the nice features of C++ is
that you can give special meanings
 to operators, when they are used
 with user-defined...
You can implement C++ operator
     overloads by providing special
member-functions on your classes that
follow a particul...
1. Read the following function definition :
void print(int x)
{
    cout << x;
}
void print (float x)
 {
    cout << x;
}
...
a. All these functions can be used in the same
   program. Explain this feature of C++
b. Write sample statements to invok...
2. Function with default arguments performs
   overloading. Explain this fact with the help
   of example.

• Functions wi...
3. Why is function overloading connected to
   compile-time polymorphism or early
   binding?

• Early binding refers to t...
Function overloading
Function overloading
Upcoming SlideShare
Loading in …5
×

of

Function overloading Slide 1 Function overloading Slide 2 Function overloading Slide 3 Function overloading Slide 4 Function overloading Slide 5 Function overloading Slide 6 Function overloading Slide 7 Function overloading Slide 8 Function overloading Slide 9 Function overloading Slide 10 Function overloading Slide 11 Function overloading Slide 12 Function overloading Slide 13 Function overloading Slide 14 Function overloading Slide 15 Function overloading Slide 16 Function overloading Slide 17 Function overloading Slide 18 Function overloading Slide 19 Function overloading Slide 20 Function overloading Slide 21 Function overloading Slide 22 Function overloading Slide 23 Function overloading Slide 24 Function overloading Slide 25
Upcoming SlideShare
Function overloading
Next
Download to read offline and view in fullscreen.

15 Likes

Share

Download to read offline

Function overloading

Download to read offline

About basics of Function overloading and Operator overloading

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all

Function overloading

  1. 1. 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
  2. 2. 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.
  3. 3. 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
  4. 4. 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.
  5. 5. 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);
  6. 6. 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.
  7. 7. 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’);
  8. 8. 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:
  9. 9. 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
  10. 10. 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
  11. 11. 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
  12. 12. 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.
  13. 13. 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.
  14. 14. 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.
  15. 15. 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);
  16. 16. 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.
  17. 17. 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+().
  18. 18. 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.
  19. 19. 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.
  20. 20. 1. Read the following function definition : void print(int x) { cout << x; } void print (float x) { cout << x; } void print(char x) { cout << x; }
  21. 21. 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’);
  22. 22. 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);
  23. 23. 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.
  • VinayDevdas

    Jan. 13, 2021
  • JyotiTotla

    Apr. 22, 2020
  • AnshikaArora9

    Apr. 22, 2018
  • HarshitSingh155

    Apr. 17, 2018
  • gk947218

    Apr. 10, 2018
  • arvindpandey36

    Jan. 24, 2018
  • riches123

    Apr. 22, 2017
  • richamahendruhello

    Apr. 21, 2017
  • richamahendru83

    Apr. 21, 2017
  • iqrajavaidrana

    Jan. 23, 2017
  • kavyashree39

    Nov. 8, 2016
  • chandni1972

    Mar. 10, 2016
  • AnjaliRathaur

    Dec. 22, 2015
  • RaghavBhatnagar2

    May. 20, 2015
  • sandhyachalam

    Jul. 12, 2013

About basics of Function overloading and Operator overloading

Views

Total views

15,219

On Slideshare

0

From embeds

0

Number of embeds

8

Actions

Downloads

1,041

Shares

0

Comments

0

Likes

15

×