Presentation is loading. Please wait.

Presentation is loading. Please wait.

Welcome to FUNCTION OVERLOADING Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS) KV jhagrakhand.

Similar presentations


Presentation on theme: "Welcome to FUNCTION OVERLOADING Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS) KV jhagrakhand."— Presentation transcript:

1

2 Welcome to FUNCTION OVERLOADING Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS) KV jhagrakhand

3 Function Overloading =>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. => The overloading are selected for invoking by matching arguments, both type(of arguments) and number(of arguments).this information is known to the compiler at the compile time and,thus, the compiler is able to select the appropriate function for a particular call at the compile time itself. This is called EARLY-BINDING or STATIC BINDING or STATIC-LINKING. It is also known as compile-time Polymorphism.

4 =>Function overloading : A function name having several definitions that are differentiable by the number or types of their arguments is known as overloading of function. =>The secret to overloading is that each redefinition of the function must use either- different types of parameters different number of parameters. float divide( int a,int b); float divide(float x,float y);

5 Function Overloading and Ambiguity void f(int x); void f(int &x); // error two functions cannot be overloaded when the only difference is that one takes a reference parameter and the other takes a normal, call-by-value parameter. => Note: Function overloading not only implements polymorphism but also reduces number of comparisons in a program and thereby makes the program run faster.

6 Declaration and definition The key to function overloading is a function’s argument list(i.e., number and type of arguments). A function’s argument list is known as its signature. Example : Different types of arguments Different number of parameters void print(int a); void print (double b); void print(char c); void area(float r); void area(float l, float b); void area( float a, float b, float c );

7 After declaring overloading function,you must redefine them separately. void area(float r) { cout<<“radius”<<r<<“area of circle”<<3.14*r*r; }

8 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.

9 When a function name is declared more than once in a program, the compiler will interpret the second(and subsequent) declarations as follows: 1. If the signature of subsequent function s match the previous function’s,then the second is treated as a re-declaration of the first. 2. If the signature of subsequent function s match exactly but the return type differ, the second declaration treated as an erroneous re- declaration of the first and is flagged at compile time as an error. float square(float f); double square(float f); //error

10 3.If the signature of two functions differ in either the number or type of their arguments, the two functions are considered to be overloaded. float square(float f); double square(double f); //allowed

11 Restriction on overloaded function 1. Any two functions in set of overload functions must have different arguments lists. 2. Overloading functions with argument lists of same types, based on return type alone, is an error. 3. Member function can’t be overloaded solely on the basis of one being static and other non-static. 4. Typedef declarations do not define new type, they introduces synonyms for existing types. They do not affect the overloading mechanism. typedef char *pstr; void print(char * pr); void print(pstr pr);

12 5. Enumerated type are distinct types and can be used to distinguish between overloaded functions. 6. The types “array of” and “ pointer to ” are consider identical for the purpose of distinguishing between overloaded functions. this is true only for single dimensioned array. void print( char *pr); void print(char pr[]);

13 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 ’);

14 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:

15 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

16 STEPS- 2 : A match through promotion If no exact match is found, an attempt is made to achieve a match through promotion of the actual argument. 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

17 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(471); //matches the function #2 int 471 is converted to float

18 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.

19 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); float f = calc(3.5, 10,30.5);

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 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 2.Why is function overloading connected to compile-time polymorphism or early binding? 3.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.

23 ADVANTAGES OF FUNCTION OVERLOADING 1.Default arguments might not work for all possible combinations of arguments whereas a function may be overloaded for all possible combinations of arguments. 2. With function overloading, multiple function definitions can be executed but with default arguments exactly one function definition is executed. 3.BY declaring an overloaded function,you save the compile time from the trouble of pushing the default argument value on the function call stack and save the function from the trouble of testing the default value.

24 END

25 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.

26 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.

27

28 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.

29 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+().

30 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.

31 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.


Download ppt "Welcome to FUNCTION OVERLOADING Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS) KV jhagrakhand."

Similar presentations


Ads by Google